JArmus is a library for detecting and preventing deadlocks. It includes support for: Thread.join , CyclicBarrier , CountDownLatch , Phaser and ReentrantLock .
To use JArmus, you need to measure your code. Either through one of its tool classes, or automatically with JArmus jarmusc .
java -jar jarmusc.jar yourprogram.jar checkedprogram.jar
Typing yourprogram.jar is the program you want to test. Conclusion - this is the same program with checks to automatically find a dead end.
Barriers need help
Checking deadlocks with the CyclicBarrier , CountDownLatch , Phaser classes is a bit complicated - for example, JConsole cannot detect these types of deadlocks. JArmus needs a little help from you: you must indicate which threads affect the synchronization, we call these registered threads.
As soon as possible, the thread should mark itself as registered. A good place to flag registered threads is the begin Runnable.run method. JArmus.register(latch);
Example
The following program, in which deadlocks are correctly identified by JArmus:
final CountDownLatch latch = new CountDownLatch(2); final CyclicBarrier barrier = new CyclicBarrier(2); final Queue<Exception> exceptions = new ArrayDeque<>(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { JArmus.register(barrier); // do not forget to register! JArmus.register(latch); // do not forget to register! latch.countDown(); latch.await(); barrier.await(); } catch (Exception e) { exceptions.add(e); } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { try { JArmus.register(barrier); // do not forget to register! JArmus.register(latch); // do not forget to register! barrier.await(); latch.countDown(); latch.await(); } catch (Exception e) { exceptions.add(e); } } }); t1.start(); t2.start();
Tiago Cogumbreiro May 5 '14 at 3:06 p.m. 2014-05-05 15:06
source share