aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Again" Watch "Threads Again" New topic
Author

Threads Again

ohimai anthony
Greenhorn

Joined: Feb 07, 2012
Posts: 15
can someone please explain what is happening in this code. I am getting funny results.

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2333
    
  28

What results are you getting? Why do you think they are funny?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18724
    
  40


Please QuoteYourSources.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ohimai anthony
Greenhorn

Joined: Feb 07, 2012
Posts: 15
the loop always runs five times before terminating instead of just 3 times.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2333
    
  28

I'm not going to give you the answer because I believe the whole point of the exercise is to make you think about it. Just giving you a clue. Look at where the variable x is declared, and then look at your output, and try to explain the behavior based on what you know about threads.
ohimai anthony
Greenhorn

Joined: Feb 07, 2012
Posts: 15
I understand that the three threads share the same variable x, since it is not local. what I don't get is - why does loop have to execute exactly five times. the result should be unpredictable. Once one of the threads gets the value of x to 3, the loops should stop.
Norbert Muench
Greenhorn

Joined: Mar 09, 2012
Posts: 19
You assume that none of the threads are preempted while running their loop. This assumption is wrong.
Matheus Souza
Ranch Hand

Joined: Mar 06, 2012
Posts: 38

ohimai anthony wrote:I understand that the three threads share the same variable x, since it is not local. what I don't get is - why does loop have to execute exactly five times. the result should be unpredictable. Once one of the threads gets the value of x to 3, the loops should stop.


I don't think that the x variable is shared. It's not an static, so it's an instance variable, isn't it?
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
I tried this and I get the different results every time
1st time

2nd time

and so on.. loop runs 7, 8 or 9 times. So, what number each thread is picking for Instance variable i ?
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2333
    
  28

The result will be so unpredictable that even the unpredictability is unpredictable. It depends a lot on how the OS schedules your threads. It may do the same thing every time, or it may do a different thing every time. It depends on what else is going on in the system, how many CPUs there are and so on.

Matheus Souza wrote:

I don't think that the x variable is shared. It's not an static, so it's an instance variable, isn't it?


x variable is shared because all threads are using the same instance of runnable.
Matheus Souza
Ranch Hand

Joined: Mar 06, 2012
Posts: 38

Jayesh A Lalwani wrote:The result will be so unpredictable that even the unpredictability is unpredictable. It depends a lot on how the OS schedules your threads. It may do the same thing every time, or it may do a different thing every time. It depends on what else is going on in the system, how many CPUs there are and so on.

Matheus Souza wrote:

I don't think that the x variable is shared. It's not an static, so it's an instance variable, isn't it?


x variable is shared because all threads are using the same instance of runnable.


Oh, that's write, Thanks =D
ohimai anthony
Greenhorn

Joined: Feb 07, 2012
Posts: 15
thank you all so so much. I guess I just have to run the program as many times as possible to see the differences.
 
 
subject: Threads Again