• 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

CertPal #33 on Threads

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It asks for the output after the code is executed:



When I read through it at first it seemed there were two separate threads being started, and since Run was synchronized and x is a member variable (initialized to 0), the first call on start would return 1, and 2. Then when the second thread started, it would also print 1 and 2.
It was wrong, and the answer seems to be 1,2,3,4 - as if there is only one instance of x. But it's not a static variable, is it? So for x to be the same variable accessed by thread1 and thread2, would it have to be the same instance of a thread? And you can't start the same thread twice, correct?

So why is x holding it's value in this code?

Thanks.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because the runnable object instance passed to both threads is same. If you had created a new runnable object and passed it to second thread then it would have been 1 2 1 2.
 
Vince Kennedy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neha Daga wrote:because the runnable object instance passed to both threads is same. If you had created a new runnable object and passed it to second thread then it would have been 1 2 1 2.


So you can have two threads with the same instance of Runnable? i.e. the Run2 r2 = (Run2)r1; isn't two instances of the same thread, but two threads with the same instance of runnable? So calling start on each is legal?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vince Kennedy wrote:So you can have two threads with the same instance of Runnable?


Yes

i.e. the Run2 r2 = (Run2)r; isn't two instances of the same thread, but two threads with the same instance of runnable? So calling start on each is legal?


The line you mentioned just creates a sort of alias to r. Neither does it create two instances of a thread, nor it creates two threads with the same runnable. It is just a plain assignment. The next two lines create two threads with the same runnable. You can simplify the code like this
 
Vince Kennedy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

i.e. the Run2 r2 = (Run2)r; isn't two instances of the same thread, but two threads with the same instance of runnable? So calling start on each is legal?


The line you mentioned just creates a sort of alias to r. Neither does it create two instances of a thread, nor it creates two threads with the same runnable. It is just a plain assignment.


Ok, but if this is just a simple assignment/alias statement, does that mean there are two references to the same thread? So how come you can start them both?

There are either two thread instances here or two references to one thread instance. If it's two thread instances, using one runnable then I suppose I understand why the output is 1 2 3 4. If it's two references to one thread, I don't understand how start can be called on the same thread through two references.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ok, but if this is just a simple assignment/alias statement, does that mean there are two references to the same thread?


A Runnable is not a Thread. You need to understand the difference. This statement of yours is correct

If it's two thread instances, using one runnable then I suppose I understand why the output is 1 2 3 4.

 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic