Created
February 22, 2014 00:31
-
-
Save bjpeterdelacruz/9146644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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