• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Synchronization lock

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class MyRunner implements Runnable{
public void run(){
doThis();
}
synchronized void doThis(){
}
}


class Test{
public static void main(String[] args){
MyRunner r1 = new MyRunner();
MyRunner r2 = new MyRunner();

Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);

t1.start();
t2.start();
}
}

A few questions related to synchronisation based on the above programme.
1. when you say t1.start(), which object is locked - t1, r1 or 'this'?
2. t2 can't execute doThis() method before t1 completes with it - yes or no? why?
3. does it make a difference to your above answer, if t1 and t2, takes the same argument, r1 in the constructors.

This might help me undertand the synchronizaion better.

Thanks in advance.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for question number1==> r1 will be locked
for question number2==> No, because the object r1 only locked not r2
for question number1==> Yes, that will make a difference because lock will be on same object

And the last thing, its better to try yourself for these type of things. In your code you are not processing any thing in method doThis().If you put something in that you will know automatically what is happening inside.That is the good thing that all the successful exam writers and authurs prefer.

--Naveen
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Naveen.

Re: your advice, e'one knows that if you try it yourself, you will learn it better. And I think people seem to try themselves before posting it to this forum. But sometimes, you can just go crazy when you are reading too much of logic and trying hard to imbibe complicated info/concepts. At times it may be a simple concept but however hard you try by thinking or coding, you fail to get a grip of it. This is exactly how it'd be ->
In such a situation this forums comes handy, I think; it worked well for me and Im sure it did for many.

You know what I mean..take it easy..I think I need a break now
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic