Note that there are some explanatory texts on larger screens.

plurals
  1. POjava concurrency - interrupt a DeadLook thread
    primarykey
    data
    text
    <p> I'm trying to learn the basics of java concurrency, I start with oracle java tutorial until <a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html</a>, I just add some line to the code example,but when I run the code, the main method never return. I know that both of alphonseThread and gastonThread are deadlocked by each ether, but why can't interrupt them from the main Thread? <pre><code>public class DeadLock { static class Freind { private final String name; public Freind(String name_) { this.name = name_; } public String getName() { return this.name; } public synchronized void bow(Freind bower) throws InterruptedException { System.out.format("%s : %s " + "has bowed to me!%n", this.name, bower.getName()); Thread.sleep(2000); bower.bowBack(this); } public synchronized void bowBack(Freind bower) { System.out.format("%s : %s " + "has bowed back to me!%n", this.name, bower.getName()); } } @SuppressWarnings("SleepWhileInLoop") public static void main(String[] args) throws InterruptedException { // wait for 4 seconds before send a interuption signal int patient = 4000; int waitingUnit = 1000; long start; final Freind alphonse = new Freind("Alphonse"); final Freind gaston = new Freind("Gaston"); // the first thread Thread alphonseThread = new Thread(new Runnable() { @Override public void run() { try { alphonse.bow(gaston); } catch (InterruptedException ex) { System.out.println("Alphnso have been killed"); } } }); // the second thread Thread gastonThread = new Thread(new Runnable() { @Override public void run() { try { gaston.bow(alphonse); } catch (InterruptedException ex) { System.out.println("Gaston have been killed "); } } }); // start the tow threads start = System.currentTimeMillis(); alphonseThread.start(); gastonThread.start(); while (alphonseThread.isAlive() || gastonThread.isAlive()) { if (((System.currentTimeMillis() - start) &lt; patient) &amp;&amp; (alphonseThread.isAlive() || gastonThread.isAlive())) { System.out.println(System.currentTimeMillis() - start); System.out.println("still waiting !!"); Thread.sleep(waitingUnit); } else { if (alphonseThread.isAlive()) { alphonseThread.interrupt(); alphonseThread.join(); } if (gastonThread.isAlive()) { gastonThread.interrupt(); gastonThread.join(); } } } System.out.println("finnaly I kill all of them"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload