java FileLock
2016-04-22 14:34
239 查看
import java.nio.ByteBuffer; import java.nio.IntBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.io.RandomAccessFile; 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; // } String temp[]={"-w","xxx.txt"}; argv=temp; 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 area void 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 work Thread.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 lock void 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 slots private 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 work Thread.sleep(500); } // leaves position and limit correct for whole buffer buffer.clear(); fc.write(buffer, INDEX_START); } // ---------------------------------------------------------------- private int lastLineLen = 0; // Specialized println that repaints the current line private 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(); } }
相关文章推荐
- rabbitmq学习11:基于rabbitmq和spring-amqp的远程接口调用
- eclipse 快捷键
- rabbitmq学习10:使用spring-amqp发送消息及异步接收消息
- rabbitmq学习8:spring-amqp的重要类的认识
- struts前台向后台传递map问题
- Spring+Struts2+Mybatis
- Java annotation
- rabbitmq学习1:hello world
- Java 读取扫描枪
- Spring中基于Schema的AOP配置详解
- java 统计代码行数
- java中的IO二
- 在AndroidStudio中使用Eclipse下的CodeFormates和CodeTemplates
- JAVA基础知识备忘
- 逻辑运算符,if、swtch语句(java基础知识三)
- 基于Java内存溢出的解决方法详解
- Java编程题:井底一只青蛙,白天向上爬5米,晚上向下掉3米,写出在任意井高都能准确算出爬行天数的程序
- spring AOP
- 简单整合Spring+SpringMVC+Mybatis
- 【java】使用Stanford CoreNLP处理英文(词性标注/词形还原/解析等)