java中nio独占锁共享锁交互
来源:互联网 发布:剑三成男捏脸数据大 编辑:程序博客网 时间:2024/05/20 09:07
java,nio中独占锁,共享锁交互。
代码使用共享锁实现了reader进程,使用独占锁实现了writer进程。由于锁是与进程而不是Java线程关联的,您将需要运行该程序的多个拷贝。先从一个writer和两个或更多的readers开始,我们来看下不同类型的锁是如何交互的。
package nio.test;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.IntBuffer;import java.nio.channels.FileChannel;import java.nio.channels.FileLock;import java.util.Random;/** * * Test locking with FileChannel. * Run one copy of this code with arguments * "-w /tmp/locktest.dat" * and one or more copies with "-r /tmp/locktest.dat" * to see the * interactions of exclusive and shared locks. Note how too many * * readers can starve out the writer. * Note: The filename you provide will be * overwritten. Substitute * an appropriate temp filename for your favorite OS. * ** Created April, 2002 * @author Ron Hitchens (ron@ronsoft.com) *///共享锁同独占锁交互public class LockTest {private static final int SIZEOF_INT = 4;private static final int INDEX_START = 0;private static final int INDEX_COUNT = 10;private static final int INDEX_SIZE = INDEX_COUNT * SIZEOF_INT;private ByteBuffer buffer = ByteBuffer.allocate(INDEX_SIZE);private IntBuffer indexBuffer = buffer.asIntBuffer();private Random rand = new Random();public static void main(String[] argv) throws Exception {boolean writer = false;String filename;if (argv.length != 2) {System.out.println("Usage: [ -r | -w ] filename");return;}writer = argv[0].equals("-w");filename = argv[1];RandomAccessFile raf = new RandomAccessFile(filename, (writer) ? "rw" : "r");FileChannel fc = raf.getChannel();LockTest lockTest = new LockTest();if (writer) {lockTest.doUpdates(fc);} else {lockTest.doQueries(fc);}}// ----------------------------------------------------------------// Simulate a series of read-only queries while// holding a shared lock on the index areavoid doQueries(FileChannel fc) throws Exception {while (true) {println("trying for shared lock...");FileLock lock = fc.lock(INDEX_START, INDEX_SIZE, true);int reps = rand.nextInt(60) + 20;for (int i = 0; i < reps; i++) {int n = rand.nextInt(INDEX_COUNT);int position = INDEX_START + (n * SIZEOF_INT);buffer.clear();fc.read(buffer, position);int value = indexBuffer.get(n);println("Index entry " + n + "=" + value); // Pretend to be// doing some workThread.sleep(100);}lock.release();println("<sleeping>");Thread.sleep(rand.nextInt(3000) + 500);}}// Simulate a series of updates to the index area// while holding an exclusive lockvoid doUpdates(FileChannel fc) throws Exception {while (true) {println("trying for exclusive lock...");FileLock lock = fc.lock(INDEX_START, INDEX_SIZE, false);updateIndex(fc);lock.release();println("<sleeping>");Thread.sleep(rand.nextInt(2000) + 500);}}// Write new values to the index slotsprivate int idxval = 1;private void updateIndex(FileChannel fc) throws Exception {// "indexBuffer" is an int view of "buffer"indexBuffer.clear();for (int i = 0; i < INDEX_COUNT; i++) {idxval++;println("Updating index " + i + "=" + idxval);indexBuffer.put(idxval);// Pretend that this is really hard workThread.sleep(500);}// leaves position and limit correct for whole bufferbuffer.clear();fc.write(buffer, INDEX_START);}// ----------------------------------------------------------------private int lastLineLen = 0;// Specialized println that repaints the current lineprivate void println(String msg) {System.out.print("\r ");System.out.print(msg);for (int i = msg.length(); i < lastLineLen; i++) {System.out.print(" ");}System.out.print("\r");System.out.flush();lastLineLen = msg.length();}}
摘选自:Java_NIO_中文版.pdf
- java中nio独占锁共享锁交互
- java并发-独占锁与共享锁
- java并发-独占锁与共享锁
- 使用Java NIO 文件锁实现进程独占
- 独占锁和共享锁
- 多线程访问共享锁和独占锁
- 读写锁 共享读 独占写
- 基于windows api实现的共享锁/独占锁
- AQS源码分析之独占锁和共享锁
- linux C 线程池(独占共享锁实现)
- java独占锁ReenTrantLock的实现
- java NIO Socket交互
- zookeeper 分布式 独占锁
- TortoiseSVN设置独占锁
- AbstractQueuedSynchronizer:独占锁
- 同一进程中线程共享和独占的资源
- Sql server锁,独占锁,共享锁,更新锁,乐观锁,悲观锁
- Sql server锁,独占锁,共享锁,更新锁,乐观锁,悲观锁
- scrollview的一些总结
- windows下Qt5安装使用
- Webkit入门
- Dialog 透明度调整
- NDK 之 JNI数据传输
- java中nio独占锁共享锁交互
- Spring中IOC和AOP的详细解释
- 树的简单实现
- System.Xml.XmlReader编码问题
- QCAR 加载 激活 删除dataset
- unity3d 将摄像头视野保存到图片
- Android自动测试之monkeyrunner工具
- Css Sprites
- 18个扩展让你的Firefox成为渗透测试工具