// Race.java // wyscig class Counter { private int _val; public Counter(int n) { _val = n; } public void inc() { _val++; } public void dec() { _val--; } public int value() { return _val; } } class IThread extends Thread { private Counter _cnt; public IThread(Counter c) { _cnt = c; } public void run() { for (int i = 0; i < 100000; ++i) { _cnt.inc(); } } } class DThread extends Thread { private Counter _cnt; public DThread(Counter c) { _cnt = c; } public void run() { for (int i = 0; i < 100000; ++i) { _cnt.dec(); } } } class Race { public static void main(String[] args) { for (int i = 0; i < 10; i++) { Counter cnt = new Counter(0); IThread it = new IThread(cnt); DThread dt = new DThread(cnt); it.start(); dt.start(); try { it.join(); dt.join(); } catch (InterruptedException ie) { } System.out.println("value=" + cnt.value()); } } }