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(); c.stop(); } } class Clock{ ClockState _state; boolean _stop = true; TickTock _tick, _tock; public void start(){ _stop = false; _tick.start(); _tock.start(); } public void stop(){ _stop = true; } public ClockState getState(){ return _state; } public synchronized void setState(ClockState state){ _state = 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); System.out.print(_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
Java For Each Loop
쓰레드 critical section
Test processing time (loop vs. contains)
detect cycle in singly linked list
updated
4.d
Fibonacci numbers 0 - 94
Coding Challenge - 02 (Odd numbers)
Hanna Rextster.com
Геттеры и сеттеры для класса Dog