wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Thread question from ePractice - very hard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Thread question from ePractice - very hard" Watch "Doubt in Thread question from ePractice - very hard" New topic
Author

Doubt in Thread question from ePractice - very hard

Tomasz Kalkosinski
Greenhorn

Joined: Aug 25, 2008
Posts: 8
Hello Ranchers. I've came to this question on Sun's ePractice mock exam:

Answers:
  • A The output can never contain the value 10.
  • B The output can never contain the value 30.
  • C The output can never contain the value 297.
  • D The output can never contain the value 820.
  • E Making the run method un-synchronized will NOT change the possible output.

  • E is marked as a correct one, but I have doubts. I've answered C - run () is synchronized so as one thread enters it no other can enter. Hence i will be increased 10 for each thread. 297 is not possible.

    Another doubt is about unsynchronizing run. E claims that output will not change. In my opinion it can actually change. Put some sleep () before yield. While some thread is inside counting to 10 and gracefully (yield) lets other thread to enter - that thread does i++ (because i % 10 != 0) and then counts its own. So they do come concurrent i increments.

    Situation complicates - when one yields it never knows when is coming back to work. So... is 297 possible this way?

    Or maybe am I wrong somewhere? I've played with this question, added threads, counter and sleep and still there is no clear answer for me. I hope you Ranchers can help me.
    Ireneusz Kordal
    Ranch Hand

    Joined: Jun 21, 2008
    Posts: 423

    [ September 12, 2008: Message edited by: Ireneusz Kordal ]
    Alexey Pilipchuk
    Greenhorn

    Joined: Jan 30, 2006
    Posts: 9
    Hi Tomasz,

    from my point of view corerct answer is E coz the result is actualy unpredicatable, no matter if run() is synchronized or not.
    First of all we access static i variable from unsynchronized main method wich doesn't guaranty any predicable results after all event despite run is synchronized it doesn't work the way it is supposed to coz we create 100 different objects and every single thread accesses its own NoGo object without blocking others.

    So my answer is E

    brgds
    Alexey Pilipchuk
    Greenhorn

    Joined: Jan 30, 2006
    Posts: 9
    OOP'S my fault we don't create 100 and running thread does block others.

    Anyway we read i form not synchronized block, so every time we come up with different figures.

    brgds
    Don Solomon
    Ranch Hand

    Joined: Jul 20, 2008
    Posts: 48
    A sleep or yield does not allow another thread to enter a synchronized block. The "criticalsection" is still protected.


    Software development is an exercise in thinking not coding.
    Ninad Kulkarni
    Ranch Hand

    Joined: Aug 31, 2007
    Posts: 787

    Actually question is as follows in the given Sun's Free Proficiency Assessment System



    The logic behind this is the given program always prints even number
    not odd number which is not divisible by 10
    so option c is correct
    when number is odd then then it is increamented by 1 value other wise no increament happens to value of i

    Don't think this question as very hard or very easy
    the only thing is you have to think more on this question
    always understand question and think properly you will get result

    The difference in above code and your code is location of print statement of value i

    Regards
    Ninad

    [ September 14, 2008: Message edited by: Ninad Kulkarni ]

    [ September 14, 2008: Message edited by: Ninad Kulkarni ]

    [ September 14, 2008: Message edited by: Ninad Kulkarni ]
    [ September 14, 2008: Message edited by: Ninad Kulkarni ]

    SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
    Tomasz Kalkosinski
    Greenhorn

    Joined: Aug 25, 2008
    Posts: 8
    Ninad Kulkarni: your question is different question. We discuss version I've posted. Your question changes a lot.

    As of my question I fully understand what's going on I just say it's hard to decide when you follow logic. I still don't have a good explanation on it.
    Ninad Kulkarni
    Ranch Hand

    Joined: Aug 31, 2007
    Posts: 787

    Hello
    Tomasz Kalkosinski

    Extremely Sorry If I hurted you by my previous reply
    my intention is not to hurt you
    I will reply as early as possible


    Regards
    Ninad
    V.G. Hindriksen
    Greenhorn

    Joined: Sep 15, 2008
    Posts: 9
    I agree with Alexey Pilipchuk that the results are unpredictable (though it was hard to read his answer because of the irritating "coz"). Rememebr: Java will not call "run()" the moment you create a thread! So any 101 subsequent numbers from 0 to 1010 are possible and thus answers A till D are incorrect.

    But why answer E? Because of the difference between synchronized non-static code and static code. If you synchronize non-static code, you don't have control on access to static code. The trick here is that the getting of i is not synchronized with the setting of i.

    You need to synchronize both the run-part and the setting of i (i.e. by using a local variable) to get predictable results (so answer C will be correct).

    But damn, it is a very hard question indeed!
    Ninad Kulkarni
    Ranch Hand

    Joined: Aug 31, 2007
    Posts: 787

    Originally posted by Tomasz Kalkosinski:
    Hello Ranchers. I've came to this question on Sun's ePractice mock exam:

    Answers:
  • A The output can never contain the value 10.
  • B The output can never contain the value 30.
  • C The output can never contain the value 297.
  • D The output can never contain the value 820.
  • E Making the run method un-synchronized will NOT change the possible output.

  • E is marked as a correct one, but I have doubts. I've answered C - run () is synchronized so as one thread enters it no other can enter. Hence i will be increased 10 for each thread. 297 is not possible.

    Another doubt is about unsynchronizing run. E claims that output will not change. In my opinion it can actually change. Put some sleep () before yield. While some thread is inside counting to 10 and gracefully (yield) lets other thread to enter - that thread does i++ (because i % 10 != 0) and then counts its own. So they do come concurrent i increments.

    Situation complicates - when one yields it never knows when is coming back to work. So... is 297 possible this way?

    Or maybe am I wrong somewhere? I've played with this question, added threads, counter and sleep and still there is no clear answer for me. I hope you Ranchers can help me.

    Hello
    Tomasz Kalkosinski

    The correct option is E
    because main thread is printing 101 times
    regardsless of 101 individual thread objects
    working on single NoGo object
    output is always unpredictable whether run() is synchronized or not
    question is simple but trickier one

    everytime we get different even numbers to be printed on console
    when code is synchronized


    you can run following code




    [ September 15, 2008: Message edited by: Ninad Kulkarni ]
    [ September 15, 2008: Message edited by: Ninad Kulkarni ]
    Tomasz Kalkosinski
    Greenhorn

    Joined: Aug 25, 2008
    Posts: 8
    Ninad Kulkarni: you didn't hurt my feelings. I just don't want to introduce confusion about what question we discuss here.

    Also I think you shouldn't consider program's output based on your running programs. You should provide logical deduction.

    I have doubts about yield () method when x ==4, because it's unpredictable when run () isn't synchronized.

    I also have doubts about for (int x = 0 ; x < 10 ; x++, i++). Try to increase 10 to 10000 and run some tests on unsychronized run(). Just because 10 is small you can't assume that on other machine, on other JVM counting from 0 to 9 would be the same. Maybe on your machine it's pretty fast, but on other JVM would take turns on threads every time it loops? It is hard when you think about it.
    Ninad Kulkarni
    Ranch Hand

    Joined: Aug 31, 2007
    Posts: 787

    Originally posted by Tomasz Kalkosinski:
    Ninad Kulkarni: you didn't hurt my feelings. I just don't want to introduce confusion about what question we discuss here.

    Also I think you shouldn't consider program's output based on your running programs. You should provide logical deduction.

    I have doubts about yield () method when x ==4, because it's unpredictable when run () isn't synchronized.

    I also have doubts about for (int x = 0 ; x < 10 ; x++, i++). Try to increase 10 to 10000 and run some tests on unsychronized run(). Just because 10 is small you can't assume that on other machine, on other JVM counting from 0 to 9 would be the same. Maybe on your machine it's pretty fast, but on other JVM would take turns on threads every time it loops? It is hard when you think about it.


    ok

    I am not disturbing in your discussion by my reply I just try to clear the concept
    If I am wrong somewhere in my explaination then notify me about that
    suggestions are always welcome

    Best Luck

    Regards
    Ninad

    SCJP5.0 97%
    Tomasz Kalkosinski
    Greenhorn

    Joined: Aug 25, 2008
    Posts: 8
    I think Ninad that option E doesn't say: output is unpredictable either run is synchronized or not. It says that output doesn't change. Actually it does.

    I've used your NoGo1 class. Results, synchronized run ():

    Results, normal - unsynchronized run ():

    Other unsychronized run ():

    Did you mention 34? And three 0s? What can you say about it?
    Ninad Kulkarni
    Ranch Hand

    Joined: Aug 31, 2007
    Posts: 787

    Originally posted by Tomasz Kalkosinski:
    I think Ninad that option E doesn't say: output is unpredictable either run is synchronized or not. It says that output doesn't change. Actually it does.

    I've used your NoGo1 class. Results, synchronized run ():

    Results, normal - unsynchronized run ():

    Other unsychronized run ():

    Did you mention 34? And three 0s? What can you say about it?


    1.In all situation result is unpredictable

    2.when run is synchronized each thread complete its turn to run execution
    because lock is aquired so yeild is not effective other thread can't enter to run method
    so after every thread turn the value of i is increamented in such a way that the value of i is divisible by 10 due to this if block is not executed





    3.when run is not synchronized then there is a chance that another thread may enter in to execution of run method



    3.variable i is class variable so main thread can access variable i even if any one thread of 101 available threads aquire lock on NoGo1 instance (object lock)

    4.main thread prints current value of i but we don't know which thread is in running or in runnable state

    5.it is also possible main print all 0s 101 times and then other threads get chance to run and change value of i

    so even if code is synchronized or not the output is unpredictable

    option E is correct

    hope this clears you

    and tell me if I am wrong somewhere in my explaination


    Regards
    Ninad

    -----------------
    SCJP 5.0 97%
    [ September 15, 2008: Message edited by: Ninad Kulkarni ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Doubt in Thread question from ePractice - very hard
     
    Similar Threads
    Sun Practice Exam - Concurrency > Objective 4.3
    Unsure about thread algorithm
    problem in threads(sun accesibility test)
    Threads - How to approach such a question?
    thread question from sun's Free Proficiency Assessment System