• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic