/** * Construct a scenario in which the loop in Thread0 executes exactly once. * * Construct a scenario in which the loop in Thread0 executes exactly three * times. * * Construct a scenario in which the loop in Thread0 executes exactly * infinitely often. */ public class Example1 { public static void main (String args[]) { Flag globalflag = new Flag(); Thread t0 = new Thread(new Thread0(globalflag)); Thread t1 = new Thread(new Thread1(globalflag));; t0.start(); t1.start(); } } class Thread0 implements Runnable { Flag f; Thread0 (Flag globalflag) { f = globalflag; } public void run() { while (f.flag < 1) { f.flag++; } } } class Thread1 implements Runnable { Flag f; Thread1 (Flag globalflag) { f = globalflag; } public void run() { while (f.flag >= 0 ) { f.flag--; } } } class Flag { volatile int flag = 1; }