java中的 java.util.concurrent.locks.ReentrantLock類的使用方式,concurrent.locks
實現了lock的類為:ReentrantLock介面的方式解釋:
lock()方法為擷取鎖對象,如果未擷取到鎖就一直擷取鎖。
trylock():為布爾值,返回是否擷取到了鎖,如果沒有擷取到鎖則返回false,如果擷取到了則返回true
tryLock(long timeout, TimeUnit unit):表示在指定的時間內擷取鎖,如果未擷取到,則返回false,否則返回true
unlock():為釋放鎖,使其他線程有機會執行。
lockInterruptibly():表示擷取鎖,如果線程被中斷則拋出異常。
例如代碼執行個體:
package TestThread.ThreadLockDemo;import java.util.concurrent.locks.ReentrantLock;public class TestLock {public static void main(String[] args) {ReentrantLock lock = new ReentrantLock();// 初始化lock對象Test2 test2 = new Test2("蘋果", 100);// 初始化蘋果的數量Test1 test1 = new Test1(lock, 10, test2);// 初始化線程對象Thread t1 = new Thread(test1, "線程1");// 建立線程Thread t2 = new Thread(test1, "線程2");Thread t3 = new Thread(test1, "線程3");t1.start();// 啟動線程t2.start();t3.start();}}class Test1 implements Runnable {private int count;private ReentrantLock lock;private Test2 test2;public Test1(ReentrantLock lock, int count, Test2 test2) {this.lock = lock;this.count = count;this.test2 = test2;}@Overridepublic void run() {lock.lock();// 枷鎖try {test2.DiscountApple(count);} catch (Exception e) {System.out.print("調用賣蘋果的方法發生異常!");} finally {lock.unlock();// 解鎖}}}class Test2 {private String name;int count;/** * @param name蘋果的名字 * @param count初始化蘋果的數量 */public Test2(String name, int count) {this.name = name;this.count = count;}/** * * @author 作者 E-mail: * * @date 建立時間:2017年3月24日 下午1:13:14 * @version 1.0 * @parameter * @since * @return */public void DiscountApple(int discount) {this.count = this.count - discount;System.out.println(Thread.currentThread().getName() + ":蘋果的數量為:" + this.count + ",賣掉了" + discount);}}
執行個體結果:
trylock()方法的使用示範:
package TestThread.ThreadLockDemo;import java.util.concurrent.locks.ReentrantLock;public class TestLock {public static void main(String[] args) {ReentrantLock lock = new ReentrantLock();// 初始化lock對象Test2 test2 = new Test2("蘋果", 100);// 初始化蘋果的數量Test1 test1 = new Test1(lock, 10, test2);// 初始化線程對象Thread t1 = new Thread(test1, "線程1");// 建立線程Thread t2 = new Thread(test1, "線程2");Thread t3 = new Thread(test1, "線程3");t1.start();// 啟動線程t2.start();t3.start();}}class Test1 implements Runnable {private int count;private ReentrantLock lock;private Test2 test2;public Test1(ReentrantLock lock, int count, Test2 test2) {this.lock = lock;this.count = count;this.test2 = test2;}@Overridepublic void run() {if (lock.tryLock()) {// lock.lock();// 枷鎖try {test2.DiscountApple(count);} catch (Exception e) {System.out.print("調用賣蘋果的方法發生異常!");} finally {lock.unlock();// 解鎖}} else {System.out.println(Thread.currentThread().getName() + "未擷取到鎖");}}}class Test2 {private String name;int count;/** * @param name蘋果的名字 * @param count初始化蘋果的數量 */public Test2(String name, int count) {this.name = name;this.count = count;}/** * * @author 作者 E-mail: * * @date 建立時間:2017年3月24日 下午1:13:14 * @version 1.0 * @parameter * @since * @return */public void DiscountApple(int discount) {this.count = this.count - discount;System.out.println(Thread.currentThread().getName() + ":蘋果的數量為:" + this.count + ",賣掉了" + discount);}}
測試結果為: