Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>A and C</strong> (assuming the question is <em>Which of these answers are possible outputs?</em>)</p> <p>The hard part, of course, isn't when you find a possible solution. But rather, its to look hard at the ones that you think are <strong>not</strong> possible and try to convinced yourself that you've got a solid reason why its not possible <strong>and</strong> that you've eliminated all the ways to get around your reason.</p> <blockquote> <p>So far my approach has been to write down in English what might be happening in each thread ...</p> </blockquote> <p>You need to figure out which thread printed each number. Below is the most efficient, succinctness format I could think of represent that and make it easy to crossout/erase/write-over as you work through possibilities. Realize:</p> <ul> <li><p>Once you find an possible answer <strong>move on</strong>. It doesn't matter if it isn't likely in the real world or that there may be other possible (or impossible) combinations. As long as you found 1 possibility, that's all you need to <strong>move on</strong>.</p></li> <li><p>Try the simplest approach first, e.g. assume T1 for each number until you hit a number that couldn't be T1, so you fill in T2, and so on.. Hopefully, you get to the end with no contradiction (or contradictions that are easy to resolve). Once you've found a possible combination, move on.</p></li> <li><p>Feel free to skip around to <em>eliminate</em> the possible ones quickly so you can focus on the likely impossible ones.</p></li> </ul> <p>Here is the final edit of my scratch-paper/worksheet (appended with my mental annotations):</p> <pre><code>A. 0, 2, 4, 4, 6, 8, 10, 6, 1 1 1 2 2 2 2 1 &lt;- possible threads that produced this output - possible solution B. 0, 2, 4, 6, 8, 10, 2, 4, 1 2 2 2 2 ? 1 &lt;- to print second '2', T1 interrupted between L10/L11; 4 passes of T2 used up C. 0, 2, 4, 6, 8, 10, 12, 14, 1 1 1 1 2 2 2 2 &lt;- possible solution - simplest solution (T2 waits until T1 is completely done) - doesn't matter that it isn't likely, just that is possible D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, 1 2 1 2 1 2 1 2 1 2 ? &lt;- threads used up E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14, 1 1 1 1 2 2 2 2 ? &lt;- threads used up </code></pre> <p>Note:</p> <p><a href="http://download.oracle.com/javase/tutorial/essential/concurrency/atomic.html" rel="noreferrer">http://download.oracle.com/javase/tutorial/essential/concurrency/atomic.html</a></p> <blockquote> <ul> <li>Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).</li> <li>...</li> </ul> <p>Atomic actions cannot be interleaved, so they can be used without fear of thread interference.</p> </blockquote>
 

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