java 包实现了读写锁的操作:

package com.multithread.readwritelock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import com.multithread.main.ExampleInterface;
import com.multithread.readwrite.WriteThread; public class ReadWriteLockExample extends ExampleInterface { public ReadWriteLock mReadwriteLock = new ReentrantReadWriteLock(false);
public File mFile = null;
public int mCash = ;
public CountDownLatch mLatchDown = new CountDownLatch();
private boolean mStopedFlag = false; @Override
public void startDemo() {
// TODO Auto-generated method stub
mFile = new File("H:\\Project\\SST\\123.txt");
try { mFile.createNewFile();
Executor mEcecutor = Executors.newFixedThreadPool( + );
mEcecutor.execute(new Reader("Reader1"));
mEcecutor.execute(new Reader("Reader2"));
mEcecutor.execute(new Reader("Reader3"));
mEcecutor.execute(new Reader("Reader4"));
mEcecutor.execute(new Writer("Writer1", ));
mEcecutor.execute(new Writer("Writer2", -)); mLatchDown.await();
System.out.println("[startDemo]" + "Demo down"); } catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public class Reader extends Thread { public String name = null;
boolean flag = true;
private int index = ; public Reader(String name) {
this.name = name;
} @Override
public void run() {
while (flag) {
try {
mReadwriteLock.readLock().lock();
System.out.println("[Reader]" + name + "start");
Thread.sleep((long) (Math.random() * ));
if (!mStopedFlag) {
System.out.println("[Reader]" + name + "get mcash now:"
+ mCash);
} else {
flag = false;
}
Thread.sleep((long) (Math.random() * ));
System.out.println("[Reader]" + name + "down");
mReadwriteLock.readLock().unlock();
Thread.sleep((long) (Math.random() * ));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
mLatchDown.countDown(); } } public class Writer extends Thread { public String name = null;
boolean flag = true;
private int index = ;
private int cash = ; public Writer(String name, int addcash) {
this.name = name;
this.cash = addcash;
} @Override
public void run() {
System.out.println("[Writer]" + name + "start");
while (flag) { try {
mReadwriteLock.writeLock().lock();
System.out.println("[Writer]" + name + "start");
Thread.sleep((long) (Math.random() * ));
int oldcash = mCash;
if (mCash <= ) {
flag = false;
mStopedFlag = true;
} else {
mCash += cash;
System.out.println("[Writer]" + name
+ "operator cash old:" + oldcash + " To:"
+ mCash);
}
Thread.sleep((long) (Math.random() * ));
System.out.println("[Writer]" + name + "down");
mReadwriteLock.writeLock().unlock();
Thread.sleep((long) (Math.random() * ));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
mLatchDown.countDown();
} } }

读写锁的特点就是:

1.写者与读者是互斥的

2.每个写者之间是互斥的

3.读者之间可以同时进行

05-11 22:01