Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My approach to multi-threaded problems is to break up the code that the thread will run and then determine how many threads will run that code and if the access any variables that other threads could potentially use. </p> <p>In the example, there are 3 threads. The <code>main</code> thread calls <code>new Threads1().go();</code>, creates <code>r1</code>, and starts 2 more threads, <code>new Thread(r1).start();</code> and <code>new Thread(r1).start();</code>. Now that we know how many threads we can track what they are going to do.</p> <p>The <code>main</code> thread is going to die after it returns from <code>go()</code>.</p> <p>The other 2 threads are each going to enter the <code>run()</code> method of the <code>Runner</code> object, <code>r1</code>. Now what we also know is that each thread will execute <code>run()</code>. So lets look at what <code>run()</code> does. It has a <code>for</code> loop that prints output each time it executes the loop. Therefore the call to <code>run()</code> will print <code>4</code> times. So 2 threads each thread will print <code>4</code> times. Therefore the output cannot have any more than <code>8</code> numbers.</p> <p>Regarding what those digits will be is not really going to be possible since the <code>Runner</code> instance will be the same for each thread the <code>x</code> variable can change based on the other thread that is also calling <code>run()</code>. Therefore all you need to determine is, is the sequence of digits possible? The answer to that question is 'yes' for A and C. This is due to the fact that you have no idea when each thread will be preempted by the other and since during the loop there is a local copy being made, you could get some very unique orderings.</p> <p>As mentioned below by SB the, option B even though it has 8 outputs it is not possible. Try and come up with a thread sequence to create that output.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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