import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ConcurrentLocksExample { private static Lock lock = new ReentrantLock(); private static int counter = 0; public static void main(String[] args) { Thread thread1 = new Thread(() -> { for (int i = 0; i < 1000; i++) { lock.lock(); try { counter++; } finally { lock.unlock(); } } }); Thread thread2 = new Thread(() -> { for (int i = 0; i < 1000; i++) { lock.lock(); try { counter--; } finally { lock.unlock(); } } }); thread1.start(); thread2.start(); try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Counter: " + counter); } }


上一篇:
下一篇:
切换中文