• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Synchronization

 
Mani Venkata Kanth
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the answer given in mock exam :
code will print 1 to 10 two times but not in particular order.

is this answer correct? if yes please explain.

i think the answer is: code will print 1 to 10 in sequence order.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the synchronization here is not going to work like you want.

Notice that the method being synchronized is an instance method.

When you create difference instances of the class, they will each have their own instance methods.

Locking one instance of a class does not prevent the other instances methods from executing because they are seperate.
 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, That will Not work. because you are creating two objects and The main synchronisation is for the synchronised access in single object .So creating two instances will bang the purpose.
 
Mani Venkata Kanth
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thanks for replies..

but i tried it in my system...
output is:
1-10 twice in order.

please give me some more explanation
 
Rohit Suman
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi you have created two objects of class A. when the synchronized method is called it will aquire the lock of one object say Object A as both threads run both will be aquire the lock of different objects and the instance variable state of two different object will be printed. You may get the output 1 to 10 twice since the loop is small change the loop to some bigger number then see the output and its of to thread scheduling how much time a thread will get to run. hope this clear your's doubt
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by k pandu:
hi
thanks for replies..

but i tried it in my system...
output is:
1-10 twice in order.

please give me some more explanation


Try putting Thread.yield() in the for-loop expression. You will (probably) get a different answer. By doing that you give the other thread a chance to run.
 
Dave Reinhardt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
questions about this code:
1. If you passed a single a1 thread to the two threads t and t1, would it then print 1-10 twice one after the other? That's what I would assume since now there are two threads working on a single runnable. see below

A a1=new A();
Thread t=new Thread(a1);
Thread t1=new Thread(a1);
t.start();
t1.start();

2. I know that Thread implements Runnable, and so that is probably why you can pass another thread to a thread constructor, but why would this method be preferable over creating a Runnable instance and passing it instead? It seems overly confusing to pass threads to threads, so I'm wondering if this design is real-world or just contrived for an exam question? In the first example are there still only two threads (ignoring main)since start is never called on a1 and a2?
 
Rohit Suman
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi dave
its no problem passing the thread to thread constructor since the there is constructor taking Runnable argument and this constructor will be called as Thread implements Runnable you have mentioned
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Reinhardt:
questions about this code:
1. If you passed a single a1 thread to the two threads t and t1, would it then print 1-10 twice one after the other? That's what I would assume since now there are two threads working on a single runnable. see below

A a1=new A();
Thread t=new Thread(a1);
Thread t1=new Thread(a1);
t.start();
t1.start();


2. I know that Thread implements Runnable, and so that is probably why you can pass another thread to a thread constructor, but why would this method be preferable over creating a Runnable instance and passing it instead? It seems overly confusing to pass threads to threads, so I'm wondering if this design is real-world or just contrived for an exam question? In the first example are there still only two threads (ignoring main)since start is never called on a1 and a2?



2. It's contrived alright. The thread objects a1 and a2 are only good for their Runnableness. There are two threads and they are referenced by t and t1.

1. If you use just one of the objects to synchronize on, notice that the instance variable count is shared and it's that that is being incremented and printed. So you will get 1 to 20 printed not two times 1 to 10.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic