About Question enthuware.ocpjp.v11.2.3363 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

About Question enthuware.ocpjp.v11.2.3363 :

Post by hsnclk »

What if we changed the order of Data objects like below; would a livelock or deadlock occur?

Code: Select all

new Thread(() -> {w1.write(d1,d2);}).start();
new Thread(() -> {w2.write(d2,d1);}).start(); 

admin
Site Admin
Posts: 10382
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3363 :

Post by admin »

what do you think?

dameest
Posts: 16
Joined: Sat Nov 30, 2024 5:27 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3363 :

Post by dameest »

hsnclk wrote:
Sat Jan 13, 2024 12:12 pm
What if we changed the order of Data objects like below; would a livelock or deadlock occur?

Code: Select all

new Thread(() -> {w1.write(d1,d2);}).start();
new Thread(() -> {w2.write(d2,d1);}).start(); 
There may be a deadlock in this situation. If thread 1 changes d1.writer while thread 2 changes d2.writer, both will be blocked on the second while loop.

dameest
Posts: 16
Joined: Sat Nov 30, 2024 5:27 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3363 :

Post by dameest »

Well, I had the correct response, but the while loop is so heavy that I think it may result in a situation where a thread running the first while when D1.writer is already set, will cause the other thread to starve...

Suppose that Thread 1 sets D1.writer then moves to the remaining code. Thread 2 will then enter the first while loop waiting for D1.writer to become null. It is not guaranteed that Thread 2 will not be the only one thread to aquire D1 monitor for a while, because as soon as it gets out of the "own" method, it will again enter in a race with other thread to get D1 monitor and may acquire it (this is why having such intensive while on a synchronized method is a bad idea). I dont think "synchronized" keyword guarantees a fair sharing of the lock among racing Thread. What do you think?

admin
Site Admin
Posts: 10382
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3363 :

Post by admin »

Yes, "synchronized" doesn't guarantee a fair sharing of the lock among racing Thread. Fair sharing of locks depends a lot on how the actual code in written and what it is doing.
In the code given in this question however, livelock is not possible because thread t1 doesn't try to reacquire the lock to d1 once it releases the lock. So, there is only one thread that is in contention to acquire d1's lock (ie. t2) after t1 releases its lock.

dameest
Posts: 16
Joined: Sat Nov 30, 2024 5:27 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3363 :

Post by dameest »

admin wrote:
Sun Dec 22, 2024 3:57 am
In the code given in this question however, livelock is not possible because thread t1 doesn't try to reacquire the lock to d1 once it releases the lock. So, there is only one thread that is in contention to acquire d1's lock (ie. t2) after t1 releases its lock.
There are actually 2 synchronized methods in Data class: "own" and "release". The case I described may occur when t1 tries to run the "release" method while t2 is busy with the "own" method.

admin
Site Admin
Posts: 10382
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3363 :

Post by admin »

Ah right, I see your point. This case is not considered in the answer. It should be fixed.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests