Skip to content

Instantly share code, notes, and snippets.

@bjpeterdelacruz
Created February 22, 2014 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjpeterdelacruz/9146644 to your computer and use it in GitHub Desktop.
Save bjpeterdelacruz/9146644 to your computer and use it in GitHub Desktop.
/**
* Tests the locking mechanism in this class.
*
* @param args None.
* @throws Exception If there are problems.
*/
public static void main(String... args) throws Exception {
ExecutorService threadPool = Executors.newFixedThreadPool(5);
final CountDownLatch latch = new CountDownLatch(5);
File file = new File(FileUtils.getProperty("db.location", Constants.PROPERTIES_FILE));
final Data data = new Data(file);
Runnable task = new Runnable() {
@Override
public void run() {
try {
for (int index = 0; index < 10; index++) {
long cookie = data.lock(1);
Thread.sleep(1000);
data.unlock(1, cookie);
}
}
finally {
latch.countDown();
}
}
};
for (int index = 0; index < 5; index++) {
threadPool.execute(task);
}
latch.await();
threadPool.shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment