• 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

Interview question regarding synchronization

 
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic