25,985
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
public class ReadWriteLockTest {
private ArrayList<String> container = new ArrayList<String>();
private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private ReadLock readLock = readWriteLock.readLock();
private WriteLock writeLock = readWriteLock.writeLock();
public void execute(){
for(int k=0;k<1;k++){
Thread writeThread = new Thread(new Runnable(){
@Override
public void run() {
while(true){
writeLock.lock();
try{
for(int j = 0;j<3;j++){
container.add("_"+j);
}
System.out.println(Thread.currentThread().getName()+" write size:"+container.size());
}
finally{
writeLock.unlock();
}
try {
System.out.println(Thread.currentThread().getName()+" sleeping...");
TimeUnit.SECONDS.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
writeThread.start();
}
for(int x=0;x<2;x++){
Thread readThread = new Thread(new Runnable(){
@Override
public void run() {
while(true){
readLock.lock();
try{
TimeUnit.SECONDS.sleep(1);
while(container.size()>0){
String str = container.get(0);
synchronized(container){ // remove operation need exclusive lock when read-read
container.remove(str);
}
System.out.println(Thread.currentThread().getName()+" read data:"+str+ " size:"+ container.size());
}
} catch (Exception e){
e.printStackTrace();
}finally{
readLock.unlock();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
readThread.start();
}
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws Exception {
ReadWriteLockTest test = new ReadWriteLockTest();
test.execute();
TimeUnit.SECONDS.sleep(100000);
}
}
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
public class ReadWriteLockTest {
private ArrayList<String> container = new ArrayList<String>();
private int maxNum = 10;
public void execute(){
for(int k=0;k<1;k++){
Thread writeThread = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try{
while(container.size()<maxNum){
synchronized(container){
for(int j = 0;j<3;j++){
if (container.size()<maxNum) {
container.add("_"+j);
TimeUnit.SECONDS.sleep(1);
System.out.println(Thread.currentThread().getName()+" write size:"+container.size());
}
}
container.notifyAll();
System.out.println("notify read thread");
if (container.size()==maxNum) {
System.out.println(Thread.currentThread().getName()+" sleeping...");
container.wait();
}
}
TimeUnit.SECONDS.sleep(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
writeThread.start();
}
for(int x=0;x<2;x++){
Thread readThread = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try{
while(container.size()>0){
synchronized(container){
int index = container.size();
if (index>0) {
String str = container.get(0);
container.remove(0);
TimeUnit.SECONDS.sleep(1);
System.out.println(Thread.currentThread().getName()+" read data:"+str+ " size:"+ container.size());
if (index==1) {
container.notifyAll();
System.out.println("notify write thread");
}
} else {
container.wait();
}
}
}
} catch (Exception e){
e.printStackTrace();
}
}
}
});
readThread.start();
}
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws Exception {
ReadWriteLockTest test = new ReadWriteLockTest();
test.execute();
TimeUnit.SECONDS.sleep(100000);
}
}
改别人代码好痛苦。
多线程 生产者消费者
http://blog.csdn.net/shijinupc/article/details/7250407