File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question on Thread and Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on Thread and Runnable" Watch "Question on Thread and Runnable" New topic
Author

Question on Thread and Runnable

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Hi,
When I run this code, the output that I get is b=5 but the supposedly correct answer should be 'the value cannot be determined'. Why is that so?



Thanks for your replies.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Please tell us where the question comes from.

Anybody else for the answer, please?
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Hi Campbell,

The question came from /examworx.com/. I'm sure some of the questions in there are actual exam questions. Even in this forum itself, I see some ads by google advertising exam questions for sale. I guess it's not a secret anymore, it's an "open-secret" thing.

I got these questions from friends. When it's readily available, it's really hard not to take a peep at it. (I wonder who do we put the blame on?).

Thanks for your reply. I understand the terms of this forum.

Originally posted by Campbell Ritchie:
Please tell us where the question comes from.

Anybody else for the answer, please?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
I think it is because you might get the result of a before or after the reassignment in the run() method. You can't tell whether it is before or after, so you might get 5 or 20. You can't tell whether the this.a = 20; or the int b = testA.a; bit will be executed first.

JavaRanch could get in trouble under copyright legislation if people post unattributed work. And as you know we don't allow known questions or adverts for them in postings. I had a quick look at examworx and they don't appear (at a 1-second glance) to sell real questions. Your browser on your PC might be adding the Google adverts; I don't get them myself.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
[Campbell@queeg applications]$ gedit ImplementsRunnable.java&
[1] 3167
[Campbell@queeg applications]$ javac ImplementsRunnable.java
[1]+ Done gedit ImplementsRunnable.java
[Campbell@queeg applications]$ java ImplementsRunnable
b = 5
[Campbell@queeg applications]$ java ImplementsRunnable
b = 5
[Campbell@queeg applications]$ java ImplementsRunnable
b = 5
[Campbell@queeg applications]$ java ImplementsRunnable
b = 5
[Campbell@queeg applications]$ java ImplementsRunnable
b = 5
[Campbell@queeg applications]$ java ImplementsRunnable
b = 5
[Campbell@queeg applications]$ java ImplementsRunnable
b = 5
[Campbell@queeg applications]$ java ImplementsRunnable
b = 20
[Campbell@queeg applications]$ java ImplementsRunnable
b = 5
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Hi Campbell,
Thanks for the explanations.

Originally posted by Campbell Ritchie:
I think it is because you might get the result of a before or after the reassignment in the run() method. You can't tell whether it is before or after, so you might get 5 or 20. You can't tell whether the this.a = 20; or the int b = testA.a; bit will be executed first.


Threads are really unpredictable. It's also difficult to understand. I need to read the chapter again a few more times. :-)

Originally posted by Campbell Ritchie:
JavaRanch could get in trouble under copyright legislation if people post unattributed work. And as you know we don't allow known questions or adverts for them in postings.


I truly understand.

Thanks again.

edit: formatting.
[ September 10, 2008: Message edited by: LS chin ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
The unpredictable thing about the two threads is that they run idependently of each other; the "main" thread doesn't "know" that the other thread might change the value of a and the other thread doesn't "know" that the "main" thread wants to print a.

So the two threads, as demonstrated, might carry out those actions in any order.
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Originally posted by Campbell Ritchie:
The unpredictable thing about the two threads is that they run idependently of each other; the "main" thread doesn't "know" that the other thread might change the value of a and the other thread doesn't "know" that the "main" thread wants to print a.

So the two threads, as demonstrated, might carry out those actions in any order.


Ahhhhh I see! They are running independently of each other. That's why we need to sync them with locks if we want predictable results. okay. Now, I just need to imagine what a lock looks like. heheh

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Thread and Runnable
 
Similar Threads
Using super and this in the constructor together
When the a, and b are eligible for GC?
About Synchronization in Threads
SCJP / strange behaviour when using Thread-Constructor