• 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

Threads Again

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can someone please explain what is happening in this code. I am getting funny results.

 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What results are you getting? Why do you think they are funny?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please QuoteYourSources.
 
ohimai anthony
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the loop always runs five times before terminating instead of just 3 times.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You assume that none of the threads are preempted while running their loop. This assumption is wrong.
 
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic