Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class MultiThreadedCounter implements Runnable {
- private static volatile int counter = 0;
- @Override
- public void run() {
- counter = counter + 5;
- counter = counter - 5;
- }
- public static void main(String[] args) throws InterruptedException {
- ExecutorService es = Executors.newCachedThreadPool();
- for (int i = 0; i < 20000; i++)
- es.execute(new MultiThreadedCounter());
- es.shutdown();
- es.awaitTermination(10, TimeUnit.MINUTES);
- System.out.println(counter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement