File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Again

 
ohimai anthony
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone please explain what is happening in this code. I am getting funny results.

 
Jayesh A Lalwani
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What results are you getting? Why do you think they are funny?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
ohimai anthony
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the loop always runs five times before terminating instead of just 3 times.
 
Jayesh A Lalwani
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You assume that none of the threads are preempted while running their loop. This assumption is wrong.
 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Eclipse IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all so so much. I guess I just have to run the program as many times as possible to see the differences.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic