java - How do you think through and predict the output of a threading question like this? -


I am ready for SCJP, and multithreading is my destiny area, mostly because I do not know how to look at Multithreading code And walk through it. So far my perspective has been written in English that what is happening in every thread and can test some cases, which irregularly interferes with each other, which actually hits and miss and time consuming The approach is. So I want to see how a supporter will be known about it. Will you be ready to read the code below (this is the latest question that is bothering me) and write that comes out of your head (code-related stuff only, please :) As soon as you get the possible output? The options that come with the question are in the end. What I see is not the solution that I have, but how to solve it efficiently in the examination.

And yes, I know that this question is not the exact answer,

Question: Which of these answers are possible outputs?

Question: Which of these answers are possible outputs?

  Public square threads 1 {int x = 0; Class runner executes Runnable {public zero run () {int current = 0; {Current = x; for int i = 0; i & lt; 4; i ++) System.out.print (current + ","); X = Current + 2; }}} Public static zero chief (string [] args) {new threads 1 (). Go (); } Public going to zero () {runnab r1 = new runner (;); New thread (R1). Start (); New thread (R1). Start (); }}  

option (choose all that apply):

A, 2, 4, 4, 6, 8, 10, 6,

B. 0, 2, 4, 6, 8, 10, 2, 4,

c. 0, 2, 4, 6, 8, 10, 12, 14,

D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,

E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,

A and C (assuming which of these answers are possible output? )

Of course, when you find a possible solution, then it is not a difficult part. Rather, it is hard to see those people who feel that no is possible and try to convince yourself that you have got a concrete reason why it is not possible and < / Strong> has eliminated all methods to fulfill its causes.

So far my approach is writing in English that what is happening in every thread ...

You need to find out who From each thread is printed. Below is the most efficient, short format that I can think of representing it and it can easily be easy to cross / delete / write as you work through the possibilities. Realize:

  • Once possible find the possible go . It does not matter whether it is not possible in the real world or other possible (or impossible) combinations. Unless you have 1 chance, you need to move forward .

  • Try the simplest way first, e.g. Assume T1 for each number, until you hit a number, which can not be T1, then you fills T2, and so on .. hopefully, you can solve any contradiction (or contradictions) Easy to reach). After getting a possible combination, move on.

  • Do not be afraid to leave possible people around to eliminate so that you can focus on it is probably impossible.

Here is the last edit of my scratch-paper / worksheet (attached to my mental comments):

  0, 2, 4, 4, 6, 8, 10, 6, 1 1 2 2 2 2 2 1 & lt; - Potential threads that produce this output - Potential solution B2, 2, 4, 6, 8, 10, 2, 4, 1 2 2 2 2? 1 & lt; - To print the second '2', interrupted between L10 / L11 in T1; T2's 4 pass C, 2, 4, 6, 8, 10, 12, 14, 1 1 1 1 2 2 2 2 Lieutenant - possible solution - Simple solution (T2 is done completely until T2 It does not matter if it is not possible, it is possible that D 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; - 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 The threads used? & Lt; - Sources used  

Note:

  • Reading and writing are atoms for reference variables and most Primitive variables (except for all types long and double)
  • ...

Atomic verbs can not be interfered, so the thread can be used without fear of interference. / P>


Comments