Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interview question regarding synchronization  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was asked an interview question around the usage of synchronized block, I would like to know the answers for various scenarios.. Here is the code block on which the question is based:


case1: what happens when thread1 is in m1() statement 3 and relinquishes control , while thread2 is executing m2(). I told that it can execute as there is no synchronization.
case 2: now we make method m1() as synchronized, the same scenario repeats; thread1 is in m1() statement 3 and relinquishes control , while thread2 is executing m2(). for this also I said, there is no conflict, it will work.
case 3: now we make both method m1() and m2() as synchronized. repeat the same scenario. for this too, I think the result should be same. as thread1 will relinquish control and will have to leave the lock on the instance of class, so thread2 should have no problem in execution.

I was told that my answer is somewhat wrong. I would like to know where and why my answer is wrong. unless m1() and m2() are called simultaneously, I do not believe there can be any blocking on method call..

am I right?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:case 3: now we make both method m1() and m2() as synchronized. repeat the same scenario. for this too, I think the result should be same. as thread1 will relinquish control and will have to leave the lock on the instance of class, so thread2 should have no problem in execution.

I wonder if this is a language thing. I interpret "relinquishes control" to mean "and the time slicing task schedule on the operating system switches to another thread." When this happens, the first thread keeps it's locks and the second thread can't proceed.

If "relinquish control" meant "exit the method", you'd be right.
 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. He just said relinquish control. But is it possible for a thread to relinquish control without releasing the lock on object? then what would be the benefit of thread scheduler giving control to other thread?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:Thanks for the reply. He just said relinquish control. But is it possible for a thread to relinquish control without releasing the lock on object? then what would be the benefit of thread scheduler giving control to other thread?

Let's try a real world analogy. We have three people (threads):
A(lice) - Responsible for writing a letter and handing it to Bob to mail.
B(ob) - Responsible for mailing the letter
C(harlie) - Responsible for having lunch

Let's suppose the task schedule starts with Bob. Bob realizes he can't do anything without the letter and waits (relinquishes control) of the the CPU. This lets Alice or Charlie do their job while Bob has voluntarily waited. I think the is the scenario they are thinking of.

Now let's suppose that Alice takes 20 minutes to write the letter. The task scheduler decided it would be nice to give Charlie a turn. Charlie isn't waiting for either Alice or Bob. He can run as soon as he gets a turn. I don't think this is scenario they are thinking of, but it is equally viable.
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The scheduler only schedules runnable threads. In your third example, the second thread is blocked waiting for the lock. It will not be scheduled.

Henry
 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:The scheduler only schedules runnable threads. In your third example, the second thread is blocked waiting for the lock. It will not be scheduled.

Henry


does wait() not release the lock before giving up the control to thread scheduler?
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was no mention of the wait () method in the original post. And considering that the interviewer thought you were incorrect,then perhaps it wasn't a part of the interview question as well.

BTW I think that the issue was not asking for clarification during the interview. You clearly have confusion regarding the question, you should have clarified the question first.

Henry
 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:There was no mention of the wait () method in the original post. And considering that the interviewer thought you were incorrect,then perhaps it wasn't a part of the interview question as well.

BTW I think that the issue was not asking for clarification during the interview. You clearly have confusion regarding the question, you should have clarified the question first.

Henry


Okay, looks like I did not ask for clarification and assumed incorrectly. Will make sure next time I do not have any doubts abt the question.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that interview questions are not real life. There is a convention and it matters just as much how you approach the question as what answer you give.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!