java中的 java.util.concurrent.locks.ReentrantLock類的使用方式,concurrent.locks

來源:互聯網
上載者:User

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);}}

測試結果為:

 

 

unlock()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.