admin管理员组文章数量:1599536
ReentrantLock保证了只有一个线程可以执行临界区代码。
允许多个线程同时读,但只有一个线程在写,其他线程就必须等待。
使用ReadWriteLock可以解决:
只允许一个线程写入(其他线程既不能写入也不能读取)
没有写入时,多个线程允许同时读(提高性能)
程序运行截图如下:
源码如下:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
class Counter{
private ReadWriteLock lock = new ReentrantReadWriteLock();
private Lock rlock = lock.readLock();
private Lock wlock = lock.writeLock();
private int value = 0;
public void add(int m){
wlock.lock();
try{
this.value += m;
}
finally {
wlock.unlock();
}
}
public void dec(int m){
wlock.lock();
try{
this.value -= m;
}
finally {
wlock.unlock();
}
}
public int get(){
rlock.lock();
try{
return this.value;
}
finally {
System.out.println("Count get Finally");
rlock.unlock();
}
}
}
public class Main1 {
final static int LOOP = 100;
public static void main(String[] args) throws InterruptedException {
final Counter counter = new Counter();
Thread t1 = new Thread(){
@Override
public void run() {
for(int i = 0; i < LOOP; i++){
counter.add(1);
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
for(int i = 0; i < LOOP; i++){
counter.dec(1);
}
}
};
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("COUNTER: " + counter.get());
}
}
condition中的:
await/signal/signalAll和wait/notify/notifyAll一样。
await()会释放当前锁,进入等待状态。
signal()会唤醒某个等待线程
signalAll()会唤醒所有等待线程
唤醒线程从await()返回后需要重新获得:
程序运行截图如下:
源码如下:
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class TaskQueue{
final Queue<String> queue = new LinkedList<String>();
final Lock lock = new ReentrantLock();
final Condition notEmpty = lock.newCondition();
public String getTask() throws InterruptedException {
lock.lock();
try{
while(this.queue.isEmpty()){
notEmpty.await();
}
return queue.remove();
}
finally {
lock.unlock();
}
}
public void addTask(String name){
lock.lock();
try{
this.queue.add(name);
notEmpty.signalAll();
}
finally {
lock.unlock();
}
}
}
class WorkerThread extends Thread{
TaskQueue taskQueue;
public WorkerThread(TaskQueue taskQueue){
this.taskQueue = taskQueue;
}
@Override
public void run() {
while (!isInterrupted()){
String name = "";
try{
name = taskQueue.getTask();
}
catch (InterruptedException e) {
break;
}
String result = "HELLO, " + name + " !";
System.out.println(result);
}
}
}
public class Main2 {
public static void main(String[] args) throws InterruptedException {
TaskQueue taskQueue = new TaskQueue();
WorkerThread workerThread = new WorkerThread(taskQueue);
workerThread.start();
taskQueue.addTask("小明");
Thread.sleep(1000);
taskQueue.addTask("小红");
Thread.sleep(1000);
taskQueue.addTask("小刚");
Thread.sleep(1000);
workerThread.interrupt();
workerThread.join();
System.out.println("END");
}
}
本文标签: 笔记JavaconditionReadWriteLock
版权声明:本文标题:Java笔记-ReadWriteLock与Condition的使用 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728323483a1154133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论