aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads 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" Watch "Threads" New topic
Author

Threads

Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Given the code(from Enthuware practice test 3):


In this code the output is coming as:

World : 0
Hello : 0
World : 1
HEllo : 2
World : 3
Hello : 4

I am not getting how come the value of i is 0 for two times...it's a static variable so if suppose Thread t2 got access to it first (as we can see in the o/p, it's printing World 0) then after it when the access goes to Thread t2, it should print the value of i as 1...

Also, the explaination of this question says that "in total only 5 words will be printed." whereas these are 6.
Can anybody explain it to me...Thanks


Regards
Swapnil

[Andrew: put code between [code] and [/code] UBB tags]
[ June 24, 2006: Message edited by: Andrew Monkhouse ]

SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The results are unpredicted. The JVM is not guaranteed to call the run() method right way when the start() method is called, or in the order that the start() methods are called. Also, how long a thread run (time-slicing) before switching to another thread also depends on OS.

In your case, thread t1 print �Hello : 0� before thread t2 runs �i++� statement.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3299
    
    7
wise is, indeed, wise
This should be fixed.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11462
    
  94

It can be instructive to look at the byte-code generated from one of the run methods:;General break-out:;
  • Lines 0 - 5 compare the value in i to see if it is past the magic number of 5 or not.
  • Lines 7 - 31 is the System.out.println(.... statement
  • Lines 34 - 42 is the increment of i
  • Line 42 is the end of the method.

  • Now the bits you are interested in is that:;

    At line 19 Java puts the "World:; " into the StringBuffer (oooh, did you realize a StringBuffer was being used when you called "World :;" + i?)

    Line 22 gets the current value of i
    Line 25 appends that to the StringBuffer.

    Line 28 then creates a String from that, and line 31 sends that String to the println() method.

    So far so good. Now we do the increment (i++):;

    Line 34 gets the current value of i
    Line 37 specifies that we are going to increment by 1.
    Line 38 does the actual increment
    Line 39 puts the incremented value back into i.

    Looking at all that, there is a lot that can happen between when you retrieved a value of i in line 22 for printing, and when you put the updated value of i in line 39.

    As Wise Owen noted, "how long a thread run (time-slicing) before switching to another thread also depends on OS." - I would add to that that it can also depend on the version of the JVM and the manufacturer of the JVM and a lot of other factors.

    Originally posted by Paul Anil:;
    This should be fixed.
    What should be fixed?

    Regards, Andrew

    [Andrew: Removed some incorrect statements]
    [ July 01, 2006: Message edited by: Andrew Monkhouse ]

    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Swapnil Trivedi
    Ranch Hand

    Joined: Jun 06, 2006
    Posts: 106
    Thnaks a ton guys....That was awesome explaination

    Best Regards
    Swapnil
    Paul Anilprem
    Enthuware Software Support
    Ranch Hand

    Joined: Sep 23, 2000
    Posts: 3299
        
        7
    I meant this question should be fixed.
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11462
        
      94

    Originally posted by Paul Anil:
    I meant this question should be fixed.


    Ahhh. My appologies for my misunderstanding.

    Regards, Andrew
     
    Don't get me started about those stupid light bulbs.
     
    subject: Threads