JDK并发包---(1)重入锁ReentrantLock:基本使用

来源:互联网 发布:香港买mac pro 过关 编辑:程序博客网 时间:2024/06/06 02:55
import java.util.concurrent.locks.ReentrantLock;public class ReenterLock implements Runnable {public static ReentrantLock lock = new ReentrantLock();public static int i = 0;@Overridepublic void run() {for (int j = 0; j < 1000000; j++) {lock.lock();// lock.lock();try {System.out.println(Thread.currentThread().getName());i++;} finally {lock.unlock();// lock.unlock();}}}public static void main(String args[]) throws InterruptedException {ReenterLock tl = new ReenterLock();Thread t1 = new Thread(tl, "t1");Thread t2 = new Thread(tl, "t2");t1.start();t2.start();t1.join();t2.join();System.out.println(i);}}

0 0
原创粉丝点击