7677787980818283848586
final Object lock = new Object(); Thread t1 = new Thread() { public void run() { time.sleep(1); int counter = 0; long start = time.nanoseconds(); for (int i = 0; i < iters; i++) { synchronized (lock) { counter++; } }
8283848586878889909192
for (int i = 0; i < iters; i++) { synchronized (lock) { counter++; } } System.out.println("synchronized: " + ((time.nanoseconds() - start) / iters)); System.out.println(counter); done.set(true); } };
113114115116117118119120121122123
final ReentrantLock lock2 = new ReentrantLock(); Thread t3 = new Thread() { public void run() { time.sleep(1); int counter = 0; long start = time.nanoseconds(); for (int i = 0; i < iters; i++) { lock2.lock(); counter++; lock2.unlock(); }
119120121122123124125126127128129
for (int i = 0; i < iters; i++) { lock2.lock(); counter++; lock2.unlock(); } System.out.println("lock: " + ((time.nanoseconds() - start) / iters)); System.out.println(counter); done.set(true); } };