This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Synchronization" Watch "Regarding Synchronization" New topic
Author

Regarding Synchronization

Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39


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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 11, 2006
Posts: 61
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

Joined: Aug 21, 2006
Posts: 39
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

Joined: Sep 09, 2006
Posts: 37
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dave Reinhardt
Ranch Hand

Joined: Aug 07, 2006
Posts: 54
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

Joined: Sep 09, 2006
Posts: 37
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

Joined: Aug 03, 2002
Posts: 7729
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.
 
GeeCON Prague 2014
 
subject: Regarding Synchronization