package class8_1_Multithreading_start; public class ShareVariable { private int count = 0; public static void main(String[] args) { (new ShareVariable()).runInstance(); } public void runInstance() { System.out.println("Beginning main program."); Thread thread1 = new Thread(()->longLoop("First set")); thread1.start(); Thread thread2 = new Thread(()->longLoop("Second set")); thread2.start(); longLoop("Third set"); try { thread1.join(); thread2.join(); } catch (InterruptedException e) { System.out.println("One of our threads was interrupted"); } System.out.println("Final count: "+count); System.out.println("Done with runInstance()"); } /** * @param name name of the thread */ public void longLoop(String name) { int i; for (i = 0; i < 100000; i++) { synchronized (this) { count++; } } //count += i; } }