多线程学习--多线程中读写锁
来源:互联网 发布:单片机中断向量地址是 编辑:程序博客网 时间:2024/06/05 20:28
package Demo12;
import java.util.Random;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockTest {
/**
* @param args
*/
public static void main(String[] args) {
ReadWriteLockTest tl = new ReadWriteLockTest();
final Qquene3 quene =tl.new Qquene3();
for(int i=0;i<3;i++){
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
quene.get();
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
quene.put(new Random().nextInt(10000));
}
}).start();
}
}
class Qquene3{
Integer data=null;
ReadWriteLock lock =new ReentrantReadWriteLock();
public void get(){
lock.readLock().lock();
try {
Thread.sleep((long)Math.random()*1000);
System.out.println(Thread.currentThread().getName()+"Read data"+data);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
lock.readLock().unlock();
}
}
public void put(Integer data){
lock.writeLock().lock();
try {
Thread.sleep((long)Math.random()*1000);
this.data=data;
System.out.println(Thread.currentThread().getName()+"Write data"+data);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
lock.writeLock().unlock();
}
}
}
}
import java.util.Random;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockTest {
/**
* @param args
*/
public static void main(String[] args) {
ReadWriteLockTest tl = new ReadWriteLockTest();
final Qquene3 quene =tl.new Qquene3();
for(int i=0;i<3;i++){
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
quene.get();
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
quene.put(new Random().nextInt(10000));
}
}).start();
}
}
class Qquene3{
Integer data=null;
ReadWriteLock lock =new ReentrantReadWriteLock();
public void get(){
lock.readLock().lock();
try {
Thread.sleep((long)Math.random()*1000);
System.out.println(Thread.currentThread().getName()+"Read data"+data);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
lock.readLock().unlock();
}
}
public void put(Integer data){
lock.writeLock().lock();
try {
Thread.sleep((long)Math.random()*1000);
this.data=data;
System.out.println(Thread.currentThread().getName()+"Write data"+data);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
lock.writeLock().unlock();
}
}
}
}
0 0
- 多线程学习--多线程中读写锁
- 多线程学习记录3-读写锁
- c#多线程读写锁
- 多线程---读写锁
- 多线程文件读写锁
- 多线程的读写锁
- 多线程之读写锁
- 多线程中的读写锁
- Java多线程--读写锁
- 多线程之读写锁
- 多线程-读写锁
- java多线程学习笔记——读写锁(ReentrantReadWriteLock)
- Linux多线程基础学习(五)线程同步-读写锁
- Java多线程中读写锁ReadWriteLock的使用
- C++实现多线程读写锁
- C++实现多线程读写锁
- 【多线程】(十一)读写锁SRWLock
- 多线程Demo-ReentrantReadWriteLock读写锁
- 泛谈主流手游引擎
- EBS报表中使用安全性层次结构控制
- UVA 题目10739 String to Palindrome(区间DP)
- uitableViewCell 左滑多个按钮
- Android Adb指令
- 多线程学习--多线程中读写锁
- Struts2 iterator遍历集合
- 资源文件验证器--ResourceValidator
- Centos下配置node.js环境
- Exception in thread "Druid-ConnectionPool-Destory" java.lang.OutOfMemoryError: PermGen space
- 封装BerkeleyDB 2.0
- linux 简单命令笔记
- [论文笔记5]A Discriminatively Trained, Multiscale, Deformable Part Model
- 键盘按键数字对照表