• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread queston

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the following code is from enthuware,


Identify the correct statements regarding the following program:


1. This will throw an exception at run time because run() method can't be scynchronized.
2.It will not compile.
3. 0 2 4 is a possible output.
4.2 0 4 is a possible output.
5.0 2 2 is a possible output.
6.2 2 2 is a possible output.

The answer is 3.

Here is the explaination bserve that each JerkyThread has been given a unique value for data i.e. 0, 1, and 2. Now, once you start all the 3 threads, any of them may be scheduled to run any time.
The main thread loop that prints out the values, prints out the values of thread 1, thread 2, and thread 3 in that sequence. At that time, those threads might be in any state: runnable, running, or finished. In any case, the values will either be same as the original data value or data+data. Thus, first value can only be 0 or 0. Second value can be 1 or 2, third value can be 2 or 4.

Now, observe the line of code: if(jt.isDone()) System.out.println(jt.getData());
It prints value only if isDone() returns true. Otherwise, it prints nothing. isDone() returns true only if the statements data += data; and done = true; in the run() method of that instance have been executed.
Thus, 0 2 2 is not a possible output although 0 2 is.

My question bare in the explaination why

first value can only be 0 or 0

. Why the first value can not be the second or third trhead data + data which 2 or 4?

Please anyone explain to me on this question. Thank you!
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

. Why the first value can not be the second or third trhead data + data which 2 or 4?



As of this code:

for(JerkyThread jt : jta) {
if(jt.isDone()) System.out.println(jt.getData());
}



This code is like:


for(int i=0;i<jta.length;i++){
if(jta[i].isDone()) System.out.println(jt[i].getData());
}



this means first jta[0] will be called, then jta[1], and then jta[2] will be called always.

And jta[0] has value 0 passed to this method.

public JerkyThread(int x){
super(); this.data = x;
}



It will always become double of 0 so 0.

Second jta[1] will be called that has called with 1.
double of 1 is 2. so always second output will be 2. Same for jta[2].
 
reply
    Bookmark Topic Watch Topic
  • New Topic