• 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 : calculating minimum time, does the SCJP have such questions?

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from the 2nd self-assessment test from the OCP practice exams book.
Can anyone tell me how to get the minimum time for the code below :
C.The program’s minimum execution time is about 9 seconds.



If i am unable to answer questions like these, is a re-reading of the K&B threads chapter needed ?

PS : I wonder how people manage to pass the exam with 90% in two weeks to 3 months
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rahul,
you will NOT get questions regarding time calculation in threads, REMEMBER that there only a few things GUARANTEED in the execution of threads. However the question about time you are likely to encounter (To The Best Of My Knowledge) is...Lets say you specify a sleep time of 3000 in the Thread.sleep() method you are supposed to know that the thread is GUARANTEED to be out of the running state for NOT less than 3 seconds.

I hope this helps.
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In terms of this question, here's how you need to approach it.

You've got two threads. Each one calls do1() and do2() four times. Each of those methods takes at least 1 second to complete because of the sleep command.

The important point is that do2() is synchronized. The two threads can't call it at the same time. So it's got to be called 8 times in sequence....that's at least 8 seconds.

Each thread also calls do1() before do2(). So it's going to take another second to reach do2(). That's 9 seconds in total.

do1() isn't synchronized, so the two threads can call it at the same time...and one can call it at the same time as the other calls do2(). So there's nothing else to add to the minimum total time.

We don't know the exact sequence, but this is one possibility:

What is needed is the knowledge about how synchronized methods work, and the ability to trace a program, which makes it a reasonable question for them to ask.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ! The table made it clear.

Matthew Brown wrote:
do1() isn't synchronized, so the two threads can call it at the same time...



Also, i was wondering how this program works in a little more detail. Does the above imply that each thread has its own copy do1() which makes it possible for both of them to run code inside do1() at the same time, and there is only one copy of do2() shared by them (due to synchronization) , which makes waiting necessary ? Is that how getting a lock on an object works (the shared object being a runnable in this case) ? Please tell me if this is wrong.
Are such details needed to gain a better understanding of threads ?
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Sudip Bose wrote:


Does the above imply that each thread has its own copy do1() which makes it possible for both of them to run code inside do1() at the same time, and there is only one copy of do2() shared by them (due to synchronization) , which makes waiting necessary ? Is that how getting a lock on an object works (the shared object being a runnable in this case) ? Please tell me if this is wrong.
Are such details needed to gain a better understanding of threads ?.


Ikpefua wrote:


Rahul I guess your vision is the same as mine, let me confirm your analysis, this code instantiates a 'Runnable' object in line 5 which is made the 'target' of the Threads instantiated in line 6 and 7 respectively. Now consider the synchronized method as saying "Hey threads, you can only have access to me one at a time", REMEMBER that the aim of synchronized is to prevent concurrent access. You know, which ever thread invokes the do2() method first, has the lock of the object till the methods execution ends.
The do1() is NOT synchronized, hence concurrent access is allowed. I hope this helps.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:In terms of this question, here's how you need to approach it.

You've got two threads. Each one calls do1() and do2() four times. Each of those methods takes at least 1 second to complete because of the sleep command.

The important point is that do2() is synchronized. The two threads can't call it at the same time. So it's got to be called 8 times in sequence....that's at least 8 seconds.

Each thread also calls do1() before do2(). So it's going to take another second to reach do2(). That's 9 seconds in total.

do1() isn't synchronized, so the two threads can call it at the same time...and one can call it at the same time as the other calls do2(). So there's nothing else to add to the minimum total time.

We don't know the exact sequence, but this is one possibility:

What is needed is the knowledge about how synchronized methods work, and the ability to trace a program, which makes it a reasonable question for them to ask.




very helpful, thanks alot!
 
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:

The important point is that do2() is synchronized. The two threads can't call it at the same time. So it's got to be called 8 times in sequence....that's at least 8 seconds.



I get this.

Matthew Brown wrote:

Each thread also calls do1() before do2(). So it's going to take another second to reach do2(). That's 9 seconds in total.

do1() isn't synchronized, so the two threads can call it at the same time...and one can call it at the same time as the other calls do2(). So there's nothing else to add to the minimum total time.



I didn't get this part. Both the threads have to spend one second sleeping whenever they enter do1() - since this is not synchronized, they can do it in parallel, so per iteration of the loop, the combined minimum time taken by both the threads is 1 second.
For four iterations it is four seconds. Therefore the minimum execution time required by the program should be 12 seconds ?

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic