import java.util.concurrent.locks.*; class Monitor { Lock lc = new ReentrantLock(); int sum = 10; public static void main(String[] args) { Monitor im = new Monitor(); Thread t1 = new Thread(new Worker(im)); Thread t2 = new Thread(new Worker(im)); t1.start(); t2.start(); } void oek() { lc.lock(); try { sum++; } finally { lc.unlock(); } } } class Worker implements Runnable { Monitor im; Worker(Monitor im) { this.im = im; } @Override public void run() { im.oek(); } } class Worker2 extends Thread { @Override public void run() { System.out.println("Worker 2"); } }