Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
jb11.0 threads tick tock 1.0
//'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!"); Clock c = new Clock(); c.start(); try{ Thread.sleep(1); }catch(InterruptedException e){ System.out.print(e); }finally{ c.stop(); } } } class Clock{ ClockState _state; boolean _stop = true; TickTock _tick, _tock; public void start(){ System.out.println("\n start\n"); _stop = false; _tick.start(); _tock.start(); } public void stop(){ System.out.println("\n stop\n"); _stop = true; _tick.joinThr(); _tock.joinThr(); } public ClockState getState(){ return _state; } public synchronized void setState(ClockState state){ _state = state; System.out.print(" " + state); notify(); if(_stop) return; while(_state == state){ try{ wait(); }catch(InterruptedException e){ System.out.println(e); } } } public boolean isRun(){ return !_stop; } Clock(){ _tick = new TickTock(ClockState.TICK, this); _tock = new TickTock(ClockState.TOCK, this); } class TickTock implements Runnable{ ClockState _role; Clock _clock; Thread _thr; public void run(){ while(_clock.isRun()){ if(_clock.getState() != _role){ _clock.setState(_role); } } } private TickTock(ClockState role, Clock clock){ _role = role; _clock = clock; _thr = new Thread(this, "Thread " + _role); } public void start(){ _thr.start(); } public void joinThr(){ try{ _thr.join(); }catch(InterruptedException e){ System.out.println(_thr.getName() + " " + e); } } } } enum ClockState { TICK(""), TOCK("\n"); private String _end; @Override public String toString(){ return super.toString()+_end; } ClockState(String end){ _end = end; } }
run
|
edit
|
history
|
help
0
program to count number of lines
JAVA # Dizi Ortalama
Speed
Tree_inorder_recursive
How many Squares in Rectangle
Java tester
hw1p1
MergeSort
Basic LinkedList in Java
Equal_HashCode_TEST --- imporatnt concept about hashing