Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
jb11.0 threads tick tock
//'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[]) { System.out.println("Hello, World!\n"); Clock cl = new Clock(true); ClockWork cw1 = ClockWork.createWork(cl, Clock.ClockState.TICK); ClockWork cw2 = ClockWork.createWork(cl, Clock.ClockState.TOCK); try{ Thread.sleep(2); System.out.println("\n before stop "); cl.stop(); cw1.getThread().join(); System.out.println("\n cw1 stoped "); cw2.getThread().join(); System.out.println("\n cw2 stoped "); }catch(InterruptedException e){ System.out.println(e); } System.out.println("\n after stop "); } } class ClockWork implements Runnable{ private Clock _clock; private Clock.ClockState _workState; private Thread _th; private ClockWork(Clock clock, Clock.ClockState workState){ _clock = clock; _workState = workState; _th = new Thread(this); _th.setPriority(Thread.MAX_PRIORITY); } public static ClockWork createWork(Clock clock, Clock.ClockState clockState){ ClockWork cw = new ClockWork(clock, clockState); cw._th.start(); return cw; } public Thread getThread(){ return _th; } public void run(){ while(_clock.isRun()){ if(_workState == Clock.ClockState.TICK){ _clock.tick(); }else if(_workState == Clock.ClockState.TOCK){ _clock.tock(); } } } } class Clock{ public enum ClockState{ TICK, TOCK; } ClockState _curState; boolean _run; public synchronized void tick(){ _curState = ClockState.TICK; System.out.print(_curState); notify(); if(!_run) return; while(_curState == ClockState.TICK){ try{ wait(); }catch(InterruptedException e){ System.out.println(e); } } } public synchronized void tock(){ _curState = ClockState.TOCK; System.out.println(" " + _curState); notify(); if(!_run) return; while(_curState == ClockState.TOCK){ try{ wait(); }catch(InterruptedException e){ System.out.println(e); } } } Clock(boolean run){ _run = run; } public void stop(){ _run = false; } public boolean isRun(){ return _run; } public Clock.ClockState getState(){ return _curState; } }
run
|
edit
|
history
|
help
0
JAVA İLE CELCIUS HESAPLAMA - Fahrenheit to Celsius with Java
1
recursion_java
java274
// Java Coding Challenge - 06: Print out Fibonacci numbers 0 - 93
1st
Link document
jb6.6 str rec
Kochergina_2
exp 2