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

首頁 > 學院 > 開發設計 > 正文

ReentrantLock同步

2019-11-09 16:05:53
字體:
來源:轉載
供稿:網友

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

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

實現可中斷等待

ReentrantLock可以實現等待中斷,當某現在等待時間過長,可以接受中斷退出等待,如下所示:

package jalonTest;import java.util.concurrent.locks.ReentrantLock; /* write線程一直占有鎖不退出,read線程在等待時收到中斷可以退出等待*/public class BufferInterruptibly { PRivate ReentrantLock lock = new ReentrantLock(); public void write() { lock.lock(); try { long startTime = System.currentTimeMillis(); System.out.println("開始往這個buff寫入數據…"); for (;;)// 模擬要處理很長時間 { if (System.currentTimeMillis() - startTime > Integer.MAX_VALUE) { break; } } System.out.println("終于寫完了"); } finally { lock.unlock(); } } public void read() throws InterruptedException { lock.lockInterruptibly();// 注意這里,可以響應中斷 try { System.out.println("從這個buff讀數據"); } 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("讀結束"); } } class Writer2 extends Thread { private BufferInterruptibly buff; public Writer2(BufferInterruptibly buff) { this.buff = buff; } @Override public void run() { buff.write(); } }

前面提到的synchronized同步則不能實現中斷退出!

實現條件變量

package jalonTest;import java.util.concurrent.locks.*; /* Producer向緩存中寫入類容,內容寫入完畢后喚醒等待的Consumer線程*/class Info{ // 定義信息類 private String name = "name";//定義name屬性,為了與下面set的name屬性區別開 private String content = "content" ;// 定義content屬性,為了與下面set的content屬性區別開 private boolean flag = true ; // 設置標志位,初始時先生產 private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); //產生一個Condition對象 public void set(String name,String content){ lock.lock(); try{ while(!flag){ condition.await() ; } this.setName(name) ; // 設置名稱 Thread.sleep(300) ; this.setContent(content) ; // 設置內容 flag = false ; // 改變標志位,表示可以取走 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 ; // 改變標志位,表示可以生產 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實現多線程 private Info info = null ; // 保存Info引用 public Producer(Info info){ this.info = info ; } public void run(){ boolean flag = true ; // 定義標記位 for(int i=0;i<10;i++){ if(flag){ this.info.set("姓名--1","內容--1") ; // 設置名稱 flag = false ; }else{ this.info.set("姓名--2","內容--2") ; // 設置名稱 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(); // 實例化Info對象 Producer pro = new Producer(info) ; // 生產者 Consumer con = new Consumer(info) ; // 消費者 new Thread(pro).start() ; //啟動了生產者線程后,再啟動消費者線程 try{ Thread.sleep(500) ; }catch(InterruptedException e){ e.printStackTrace() ; } new Thread(con).start() ; } }

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德保县| 轮台县| 阳信县| 吉林省| 年辖:市辖区| 岑巩县| 独山县| 石柱| 海城市| 马山县| 水富县| 霍城县| 九龙城区| 视频| 甘谷县| 九寨沟县| 德兴市| 万载县| 驻马店市| 清原| 阳江市| 满洲里市| 平山县| 班玛县| 汶上县| 乌海市| 金寨县| 富民县| 桦南县| 普定县| 石台县| 寿光市| 韶关市| 皋兰县| 达尔| 蓬溪县| 武城县| 通化县| 顺义区| 上林县| 东乡族自治县|