Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: ParallelCounter.java
- * Created on: 12.06.2018
- * Created by: Cavvar
- * Package: edu.hm.cavvar.thread_examples;
- * Project: SE_2
- */
- package edu.hm.cavvar.thread_examples;
- public class ParallelCounter {
- public static int count = 0;
- public static final Object MONITOR = new Object();
- static class Incrementer extends Thread {
- private int myOwnCounter;
- @Override
- public void run() {
- for (int i = 0; i < 1000000; i++) {
- myOwnCounter++;
- synchronized (MONITOR) {
- count++;
- }
- }
- }
- }
- public static void main(String[] args) throws InterruptedException {
- Thread thread1 = new Incrementer();
- Thread thread2 = new Incrementer();
- thread1.start();
- thread2.start();
- System.out.println(thread1.isAlive());
- thread1.join();
- System.out.println(thread1.isAlive());
- thread2.join();
- System.out.println(ParallelCounter.count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement