Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
jb12.0 threads.enums
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { Signals signal = Signals.RED; System.out.println(signal); TraficLights tl = new TraficLights(); Thread t = new Thread(tl, "trafic light"); t.start(); for(int i = 0; i<10; i++){ System.out.printf("%s: %s\n", Calendar.getInstance().get(Calendar.MILLISECOND), tl.getSignal()); tl.waitChange(); } tl.stop(); } } enum Signals{ RED, YELLOW, GREEN } class TraficLights implements Runnable{ private Signals _signal; private boolean _stop; public void run(){ while(!_stop){ try{ switch(_signal){ case RED: Thread.sleep(150); break; case YELLOW: Thread.sleep(30); break; case GREEN: Thread.sleep(500); break; } }catch(InterruptedException e){ System.out.println("error, interrupted sleep"); System.out.println(e); } changeSignal(); } } synchronized private void changeSignal(){ switch(_signal){ case RED: _signal = Signals.YELLOW; break; case YELLOW: _signal = Signals.GREEN; break; case GREEN: _signal = Signals.RED; break; } notify(); } synchronized public void waitChange(){ try{ wait(); }catch(InterruptedException e){ System.out.println("error, interrupted wait"); System.out.println(e); } } public void stop(){ _stop = true; } public Signals getSignal(){ return _signal; } TraficLights(Signals signal){ _signal = signal; } TraficLights(){ _signal = Signals.RED; } }
run
|
edit
|
history
|
help
0
Nick-test
1.5
Largest prime factor
Roberto02
4.a
frequency of char
ChangePI
Preference List
Java
Graph adjacency list representation