class Counter { private Integer currentValue = 0; public Integer next() { synchronized(currentValue) { return currentValue++; } } }
class Worker implements Runnable { private boolean stop; public void run() { while(!stop) { // do something ... } } public void stop() { stop = true; } }