• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

threads synchronization and time lapse

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which are true? (Choose all that apply.)
A. Compilation fails.
B. The program’s minimum execution time is about 8 seconds.
C. The program’s minimum execution time is about 9 seconds.
D. The program’s minimum execution time is about 12 seconds.
E. The program’s minimum execution time is about 16 seconds.
F. Un-synchronizing do2() changes the program’s minimum execution time by only a few
milliseconds.

Answer: C

but my answer is 12 sec.(D)

do1() is completed by both threads in 1 sec
and as do2() is synchronized so its executed
one after the other thus both together should
take 2 sec so total 3 sec lapsed and this continues 4
times so output should be 3*4=12 sec.
but I'm wrong somewhere please tell me why and where
my understanding has gone wrong.


thnks in advance
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the time T1 running do2(), T2 try to run do2() get blocked.
When T1 finished do2(), T2 get started to run do2().
However, you didn't consider what may happen when T2 running do2().
If you draw a time line diagram, you may know why.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sawan Mishra wrote:do1() is completed by both threads in 1 sec
and as do2() is synchronized so its executed
one after the other thus both together should
take 2 sec so total 3 sec lapsed


This is true for the first pass. But what happens after the first thread completes do2()? Does it have to wait for the other thread to complete do2() as well, or can it continue on to the next iteration of do1() in parallel to do2() happening on the second thread? What happens to the overall time scheme when that happens?
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think there is only one thread. And then it takes 8 seconds to finish the job.
We can assume that both threads start simultaneously. Therefore do1() is run by both threads simultaneously and it takes 1 sec to run by both threads. As do2() is synchronized it takes 2 sec to run by both threads. And that means to execute one cycle it takes 3 seconds.
Answer => 3x4=12 seconds
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramesh Pramuditha Rathnayake wrote:Think there is only one thread. And then it takes 8 seconds to finish the job.
We can assume that both threads start simultaneously. Therefore do1() is run by both threads simultaneously and it takes 1 sec to run by both threads. As do2() is synchronized it takes 2 sec to run by both threads. And that means to execute one cycle it takes 3 seconds.
Answer => 3x4=12 seconds



After both the threads run do1 in 1 second, following is what could happen( one of the possibilities).

One of the two threads ( let us assume it is the first thread but it could be any thread ) acquires the monitor and proceeds with do2. The other thread is waiting for the monitor at this time.
The first thread is done with do2 and it proceeds on with do1. So at this time the other thread could acquire the monitor and be running do2. So both these threads are now running simultaneously as each method takes at least 1 second.

So the minimum time is 1 second ( both threads in do1) + 8 seconds ( each of the threads four times in do2) = 9 seconds.

This has already been said in the earlier responses. Sometimes we may just need to read the responses more than once/a couple of times. :-) Happens with me also at times. :-)
 
Saloon Keeper
Posts: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this one of those exam questions, of which we see many on this website?

The ones I've seen here are often such that I wonder what the examiners are trying to test.
Not, to my opinion, one's ability to apply a broad knowledge of Java to solve decent problems.
Instead, they are making up stupid tricky questions like the one here.

In this case: what's wrong with answer B? If something runs in about 9 seconds, then surely its mimimum time
is 8 seconds (or 7, or ...)? Brrr...
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In this case: what's wrong with answer B? If something runs in about 9 seconds, then surely its mimimum time
is 8 seconds (or 7, or ...)?


It will never be less than 9 seconds.
The first iteration will always be a minimum of 3 seconds for both threads to complete, then for each subsequent iteration the minimum time will be 2 seconds (because the first thread to complete iteration 1 will have already completed it's first 1 second wait of the next itration whilst the second thread is completing the second 1 second wait of the previous iteration ie after the first iteration the tasks are interlaced). Therefore you will have 1 x 3 seconds plus 3 x 2 seconds = 9 seconds. Any delays in the system due to OS activity etc will just add to the time.
 
Piet Souris
Saloon Keeper
Posts: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I know, but that's not what I meant.
I am just paying them back for their trickyness.
 
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please tell me where my line of thinking is incorrect?

I thought that in a non-static method, the thread will attempt obtain the object lock for the "this" object instance. In a static method, the thread will attempt to obtain a lock on the entire Class.

So the way I see this problem: do1() is not synchronized so both threads enter do1() at approx the same time and sleep for 1 second.

Then t1 obtains the monitor lock of the object instance of t1 for do2(). It enters do2() and sleeps for 1 second.

At the same time, t2 tries to enter do2() and attempts to obtain the object lock for t2, which is available. Therefore, both threads run concurrently.

Am I wrong in thinking that the locks of t1 and t2 are what is being acquired?
Is it actually the lock of the Runnable DMV object d what is being obtained?

I would appreciate if someone could clarify for me. Thanks so much.
 
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do2() is a method in the DMV class, and only one instance of that class is created. It is this instance that the synchronize lock applies to.

t1 and t2 are two different Thread objects, bit they were both created with the same Runnable instance (the DMV object).

Edit: there is no t2 in the question. There is a named reference to a thread thread (t1), and an unnamed reference to a thread that is started immediately but then the reference is discarded.
 
Amit Ramesh
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mike

This is very helpful.

So because do1 and do2 methods are located in the DMV class and there is only one object instance of DMV (d) that was passed to both threads, then both threads will be trying to obtain the object lock for d when they enter the synchronized do2() method.

Also I was aware that there is no thread named t2 in the program but I saw that convention being used as shorthand earlier in the thread so I followed suit. Next time I'll make sure to explicitly state when I'm using shorthand


Thanks so much!
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic