Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is not a <em><a href="http://en.wikipedia.org/wiki/Deadlock" rel="nofollow">deadlock</a></em>, since there's only one synchonization resource for all threads. </p> <p>Simply speaking <em><a href="http://en.wikipedia.org/wiki/Deadlock" rel="nofollow">deadlock</a></em> is when two threads require two resources for some actions, one thread grabs first resource, second thread grabs second resource and neither could proceed.</p> <p>In your case it is just all threads either fall into infinite sleep or infinite loop.</p> <p>All threads in your code are divided in two groups, i'll call them "writers" (those with isWrite==true) and "nullifiers" (technically they do writing too, but they always write null).</p> <p>At some point <em>"nullififer"</em> thread iterates over the array and sets all elements to null. It could be done by a single "nullifier" thread, since there's nothing that stops it from proceeding to next element of an array after setting one to null, as well as by several over the course of several iterations.</p> <p>No <em>"writer"</em> could proceed since they do objects.wait() when their current element is null. So they fall to infinite sleep.</p> <p>All <em>"nullifier"</em> threads infinitely overwrite array with nulls, over and over.</p> <p>Even if at first <em>"writer"</em> threads get more processor time, eventually they will be overrun by <em>"nullifiers"</em> since <em>"writers"</em> have stop condition, and <em>"nullifiers"</em> don't.</p> <p><strong>UPDATE:</strong> By the way you don't have to perform boolean comparison inside "if" statement. You can just write</p> <pre><code> while (!Thread.interrupted()) </code></pre> <p>Which is more readable and concise, and is a common practice.</p> <p><strong>UPDATE 2:</strong> You might try to fix by adding objects.wait() in the "nullifier's" else statement, similiar to the one in "writer's" if clause, like this:</p> <pre><code>} else { objects[index] = null; while (objects[index]==null) { System.out.println(number + ": Index " + index + " is still null, waiting..."); objects.wait(); } } </code></pre> <p>I don't know what this code is supposed to accomplish (it looks just like some random exercise), so I am not sure if solution is correct semantically, but it ought to fix the "rampaging nullifiers" problem.</p> <p><strong>UPDATE 3:</strong> If you add logging of thread type in the beggining of the loop you can see that soon enough only threads running are those with isWriter==false<br> <strong>EDIT:</strong> it's better to do logging after synchronization</p> <pre><code>while (Thread.interrupted() == false) { synchronized (objects) { System.out.println("running isWriter=" + isWriter + " thread #" + number); </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