import java.util.concurrent.locks.ReentrantLock; class Teller implements Runnable{ private TallBoks tallBoks; private ReentrantLock lock; Teller(TallBoks tb, ReentrantLock l) { tallBoks = tb; lock = l; } public void run() { for (int i = 0; i < 100000; i++) { lock.lock(); try { int forrigeTall = tallBoks.hentTall(); tallBoks.settTall(forrigeTall+1); } finally { lock.unlock(); } } } } class TallBoks { ReentrantLock lock = new ReentrantLock(); private int tall = 0; public int hentTall() { return tall; } public void settTall(int t) { tall = t; } } class TestTeller { public static void main(String[] args) { TallBoks tallBoks = new TallBoks(); ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread(new Teller(tallBoks, lock)); Thread t2 = new Thread(new Teller(tallBoks, lock)); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException e) { System.err.println(e); } System.out.println(tallBoks.hentTall()); } }