實現了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; } @Override public 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; } @Override public 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); } }
測試結果為:
unlock() |