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; private boolean _waiting = true; 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(); _waiting = false; } } 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{ while(_waiting) wait(); }catch(InterruptedException e){ System.out.println("error, interrupted wait"); System.out.println(e); } _waiting = true; } public void stop(){ _stop = true; } public Signals getSignal(){ return _signal; } TraficLights(Signals signal){ _signal = signal; } TraficLights(){ _signal = Signals.RED; } }
run
|
edit
|
history
|
help
0
2.C
tilt
dimond of stars
Search substring
Ways to form Max Heap
Smallest Multiple of N with Zeros and Ones
Extracting Value from a String that contains key value pairs
Basic_Structure
Геттеры и сеттеры для класса Dog
Bubble Sort Java