日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java笔记-多线程协调及ReentrantLock的使用

發布時間:2025/3/15 java 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java笔记-多线程协调及ReentrantLock的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里使用wait/notify去協調線程。

感覺這個比C/C++里面信號量還要好用一點。

?

使用synchronized沒有解決多線程協調問題

public synchronized String getTask(){while(queue.isEmpty()){this.wait();}return queue.remove; }

wait是在Object上的代碼,是jvm上的C代碼實現的,必須包含在synchronized中。

?

使用wait/notify線程協調機制

public synchronized void addTask(String s){this.queue.add(s);this.notify(); }public synchronized String getTask(){while(queue.isEmpty()){this.wait();}return queue.remove; }

因為wait方法會釋放鎖,其他方法就可以得到鎖,這樣就可以協調工作了。

this.notify()喚醒正在wait()的方法。

如下程序運行截圖如下:


源碼如下:

import java.util.LinkedList; import java.util.Queue;class TaskQueue{final Queue<String> queue = new LinkedList<String>();public synchronized String getTask() throws InterruptedException {while(this.queue.isEmpty()){this.wait();}return queue.remove();}public synchronized void addTask(String name){this.queue.add(name);this.notifyAll();} }class WorkerThread extends Thread{TaskQueue taskQueue;public WorkerThread(TaskQueue taskQueue){this.taskQueue = taskQueue;}@Overridepublic void run() {while(!isInterrupted()){String name = "";try{name = taskQueue.getTask();}catch (InterruptedException e){e.printStackTrace();}String result = ">" + name;System.out.println(result);}} }public class Main1 {public static void main(String[] args) throws Exception {TaskQueue taskQueue = new TaskQueue();WorkerThread worker = new WorkerThread(taskQueue);worker.start();taskQueue.addTask("AAA");Thread.sleep(1000);taskQueue.addTask("BBB");Thread.sleep(1000);taskQueue.addTask("CCC");worker.interrupt();worker.join();System.out.println("END");} }

下面是關于ReentrantLock

java.util.locks.ReentrantLock用于替代synchronized加鎖:

如下:

synchronized(lockObj){n = n + 1; }

替代為:

lock.lock(); try{n = n + 1; } finally{lock.unlock(); }

java.util.locks.ReentrantLock用于替代synchronized加鎖:

class Counter{final Lock lock = new ReentrantLock();public void inc(){lock.lock();try{n = n + 1;}finally{lock.unlock();}} }

ReentrantLock:

可重入鎖,一個線程可多次獲取同一個鎖。

lock()方法可獲取鎖。

tryLock()方法可嘗試獲取鎖,并可指定超時。

?

關鍵就是這指定超時。
如下例子!

程序運行截圖如下:

源碼如下:

import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;class Counter{private Lock lock = new ReentrantLock();private int value = 0;public void add(int m){lock.lock();try{this.value += m;}finally {lock.unlock();}}public void dec(int m){lock.lock();try{this.value -= m;}finally {lock.unlock();}}public int get(){lock.lock();try{return this.value;}finally {lock.unlock();}} }public class Main2 {final static int LOOP = 1000;public static void main(String[] args) throws InterruptedException {final Counter counter = new Counter();Thread t1 = new Thread(){@Overridepublic void run() {for(int i = 0; i < LOOP; i++){counter.add(1);}}};Thread t2 = new Thread(){@Overridepublic void run() {for(int i = 0; i < LOOP; i++){counter.dec(1);}}};t1.start();t2.start();t1.join();t2.join();System.out.println(counter.get());System.out.println("END");} }

使用tryLock()可指定超時:

public void inc() throws InterruptedException {if(lock.tryLock(1, TimeUnit.SECONDS)){try{this.value += 1;}finally {lock.unlock();}} }

ReentrantLock可以替代synchronized

ReentrantLock獲取鎖更安全

必須使用try.. finally保證正常獲取和釋放鎖

?

源碼下載地址:

https://github.com/fengfanchen/Java/tree/master/javaMuObject

總結

以上是生活随笔為你收集整理的Java笔记-多线程协调及ReentrantLock的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。