国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ReentrantLock同步

2019-11-09 14:53:59
字體:
供稿:網(wǎng)友

ReentrantLock加鎖和釋放鎖的一般形式如下

Lock lock = new ReentrantLock();//默認(rèn)使用非公平鎖,如果要使用公平鎖,需要傳入?yún)?shù)true ........ lock.lock(); try { //更新對象的狀態(tài) //捕獲異常,必要時(shí)恢復(fù)到原來的不變約束 //如果有return語句,放在這里 finally { lock.unlock(); //鎖必須在finally塊中釋放 }

實(shí)現(xiàn)可中斷等待

ReentrantLock可以實(shí)現(xiàn)等待中斷,當(dāng)某現(xiàn)在等待時(shí)間過長,可以接受中斷退出等待,如下所示:

package jalonTest;import java.util.concurrent.locks.ReentrantLock; /* write線程一直占有鎖不退出,read線程在等待時(shí)收到中斷可以退出等待*/public class BufferInterruptibly { PRivate ReentrantLock lock = new ReentrantLock(); public void write() { lock.lock(); try { long startTime = System.currentTimeMillis(); System.out.println("開始往這個(gè)buff寫入數(shù)據(jù)…"); for (;;)// 模擬要處理很長時(shí)間 { if (System.currentTimeMillis() - startTime > Integer.MAX_VALUE) { break; } } System.out.println("終于寫完了"); } finally { lock.unlock(); } } public void read() throws InterruptedException { lock.lockInterruptibly();// 注意這里,可以響應(yīng)中斷 try { System.out.println("從這個(gè)buff讀數(shù)據(jù)"); } finally { lock.unlock(); } } public static void main(String args[]) { BufferInterruptibly buff = new BufferInterruptibly(); final Writer2 writer = new Writer2(buff); final Reader2 reader = new Reader2(buff); writer.start(); reader.start(); new Thread(new Runnable() { public void run() { long start = System.currentTimeMillis(); for (;;) { if (System.currentTimeMillis() - start > 5000) { System.out.println("不等了,嘗試中斷"); reader.interrupt(); //此處中斷讀操作 break; } } } }).start(); } } class Reader2 extends Thread { private BufferInterruptibly buff; public Reader2(BufferInterruptibly buff) { this.buff = buff; } @Override public void run() { try { buff.read();//可以收到中斷的異常,從而有效退出 } catch (InterruptedException e) { System.out.println("我不讀了"); } System.out.println("讀結(jié)束"); } } class Writer2 extends Thread { private BufferInterruptibly buff; public Writer2(BufferInterruptibly buff) { this.buff = buff; } @Override public void run() { buff.write(); } }

前面提到的synchronized同步則不能實(shí)現(xiàn)中斷退出!

實(shí)現(xiàn)條件變量

package jalonTest;import java.util.concurrent.locks.*; /* Producer向緩存中寫入類容,內(nèi)容寫入完畢后喚醒等待的Consumer線程*/class Info{ // 定義信息類 private String name = "name";//定義name屬性,為了與下面set的name屬性區(qū)別開 private String content = "content" ;// 定義content屬性,為了與下面set的content屬性區(qū)別開 private boolean flag = true ; // 設(shè)置標(biāo)志位,初始時(shí)先生產(chǎn) private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); //產(chǎn)生一個(gè)Condition對象 public void set(String name,String content){ lock.lock(); try{ while(!flag){ condition.await() ; } this.setName(name) ; // 設(shè)置名稱 Thread.sleep(300) ; this.setContent(content) ; // 設(shè)置內(nèi)容 flag = false ; // 改變標(biāo)志位,表示可以取走 condition.signal(); }catch(InterruptedException e){ e.printStackTrace() ; }finally{ lock.unlock(); } } public void get(){ lock.lock(); try{ while(flag){ condition.await() ; } Thread.sleep(300) ; System.out.println(this.getName() + " --> " + this.getContent()) ; flag = true ; // 改變標(biāo)志位,表示可以生產(chǎn) condition.signal(); }catch(InterruptedException e){ e.printStackTrace() ; }finally{ lock.unlock(); } } private void setName(String name){ this.name = name ; } private void setContent(String content){ this.content = content ; } private String getName(){ return this.name ; } private String getContent(){ return this.content ; } } class Producer implements Runnable{ // 通過Runnable實(shí)現(xiàn)多線程 private Info info = null ; // 保存Info引用 public Producer(Info info){ this.info = info ; } public void run(){ boolean flag = true ; // 定義標(biāo)記位 for(int i=0;i<10;i++){ if(flag){ this.info.set("姓名--1","內(nèi)容--1") ; // 設(shè)置名稱 flag = false ; }else{ this.info.set("姓名--2","內(nèi)容--2") ; // 設(shè)置名稱 flag = true ; } } } } class Consumer implements Runnable{ private Info info = null ; public Consumer(Info info){ this.info = info ; } public void run(){ for(int i=0;i<10;i++){ this.info.get() ; } } } public class ThreadCaseDemo{ public static void main(String args[]){ Info info = new Info(); // 實(shí)例化Info對象 Producer pro = new Producer(info) ; // 生產(chǎn)者 Consumer con = new Consumer(info) ; // 消費(fèi)者 new Thread(pro).start() ; //啟動(dòng)了生產(chǎn)者線程后,再啟動(dòng)消費(fèi)者線程 try{ Thread.sleep(500) ; }catch(InterruptedException e){ e.printStackTrace() ; } new Thread(con).start() ; } }

在synchronized同步文章中利用synchronized實(shí)現(xiàn)了類似的功能。synchronized和ReentrantLock最大的差別還是在于高并發(fā)性。ReentrantLock對高并發(fā)性效率較高!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青铜峡市| 龙岩市| 元氏县| 循化| 星座| 新疆| 阜城县| 房产| 卫辉市| 井陉县| 乐山市| 晋州市| 苗栗市| 两当县| 剑阁县| 射洪县| 当涂县| 日喀则市| 山东省| 出国| 凤凰县| 神木县| 镇江市| 陆川县| 华池县| 读书| 徐水县| 高密市| 合作市| 资阳市| 合川市| 鹤峰县| 自治县| 仁寿县| 三台县| 五常市| 苍山县| 斗六市| 阳原县| 广平县| 元氏县|