FileDocCategorySizeDatePackage
LockTest.javaAPI DocExample3819Sat May 18 21:55:44 BST 2002com.ronsoft.books.nio.channels

LockTest

public class LockTest extends Object
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 operating system. Created April, 2002
author
Ron Hitchens (ron@ronsoft.com)
version
$Id: LockTest.java,v 1.2 2002/05/19 04:55:45 ron Exp $

Fields Summary
private static final int
SIZEOF_INT
private static final int
INDEX_START
private static final int
INDEX_COUNT
private static final int
INDEX_SIZE
private ByteBuffer
buffer
private IntBuffer
indexBuffer
private Random
rand
private int
idxval
private int
lastLineLen
Constructors Summary
Methods Summary
voiddoQueries(java.nio.channels.FileChannel fc)

		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);
		}
	
voiddoUpdates(java.nio.channels.FileChannel fc)

		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);
		}
	
public static voidmain(java.lang.String[] argv)


	      
		 
	
		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);
		}
	
private voidprintln(java.lang.String msg)


	// Specialized println which repaints the current line
	    
	
		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();
	
private voidupdateIndex(java.nio.channels.FileChannel fc)


	    
		 
	
		// "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 to this is really hard work
			Thread.sleep (500);
		}

		// leaves position and limit correct for whole buffer
		buffer.clear();
		fc.write (buffer, INDEX_START);