I added
Code: Select all
static AtomicInteger counter = new AtomicInteger();
Code: Select all
System.out.println("Counter from one: " + counter.getAndIncrement());
Code: Select all
System.out.println("Counter from two: " + counter.getAndIncrement());
Finally I wrapped the entire main inside a while (true) infinite loop. The purpose was to see how many interactions it would take until the deadlock. Unfortunately every run blew up from lack of memory for new threads, and only for that reason.
Below is pasted the end of one run, where over 36,000 interactions went by until the JVM croaked out of memory, without the satisfaction of a deadlock. If someone can see a mistake in the code, please let me know, I wanted to see the deadlocking fireworks. Thanks!
Counter from one: 36860
Counter from two : 36861
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread