Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread synchronization question ? 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 "Thread synchronization question ?" Watch "Thread synchronization question ?" New topic
Author

Thread synchronization question ?

Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
dear ranchers,
I have this question for a JQPlus mock. Hope someone can throw more light on this one

The output I thought was right is
Numbers 1 to 5 will be printed (none repeated or missed) in any order followed by "N Done" where N cannot be determined.
Since it is synchornized, this should be the right one.

But the correct output is
Numbers 1 to 5 will be printed (some may be repeated or missed) with "N Done" anywhere in the sequence where N cannot be determined.

The reason is that there are 25 different TestClass objects and each thread gets a separate lock on the object...but I am not very sure...any ideas??

thanks,
Roopesh.
[ September 12, 2005: Message edited by: Barry Gaunt ]
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
When you say
for(i=0;i<5;i++)
new TestClass().start(); you are creating five different objects

there are five TestClass objects,
every object in java can have one monitor or lock,
and once you obtain lock on an object no other thread can enter synchronized method of that object but here there are five TestClass object and five threads are started each one has individual identity.


say you say,

new TestClass().start();

// now consider it as thread 1 object TestClass 1
// now when you say,

new TestClass().start();

// now it is new TestClass object and another Thread (or different thread)
// it is thread 2 object TestClass 2

so even first TestClass object thread 1 enters synchronized code by obtaining lock of TestClass object 1 the next thread 2 can enter synchronized code as it obtains lock on its TestClass object different than first object.

may be you can do this thing

for(i=0;i<5;i++)
{
TestClass a = new TestClass();
a.setName("Thread " + i);
a.start();
}

and inside run method
give code
System.out.print(Thread.currentThread().getName());

you will see different thread names.
[ September 12, 2005: Message edited by: Santana Iyer ]
Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
Thanks Sanatana,
That surely helps..I see the point now - so the following code


is different from the one that was mentioned earlier. Here 25 different threads are competing to obtain the lock for a single ThreadClass object, whereas the earlier code there are 25 different ThreadClass objects.
Am I right?

Roopesh.
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
yes see you specify target that is object implements Runnable
so still there are many threads but target is one
so if run method is synchronized than only one thread can enter
method as it must obtain lock on object.
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
sanatana ,

why is then the main thread's threadcounter is printing 0??
i think iam losing some in my basics.please help.

srikanth


Thanks & Regards<br /> <br />-Srikanth
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
Look main is also a thread so it is possible that it gets executed before other thread.

example

class xyz extends Thread
{
String name;

public xyz(String s)
{
name=s;
}

public void run()
{
System.out.print(name);
}

public static void main(String args[]) throws Exception
{
Thread t=new xyz("First");
Thread t1=new xyz("Second");
t.start(); // 1
// here
t1.start(); // 2
// some
System.out.print("Third");
}
}
program prints three words First Second Third but
order is not guaranteed for First Second Third,
because when you start t and t1 main thread is also running it is possible that main thread runs and completes before t or t1 and printing
Third First Second or Third Second First (depending on which t ot t1 runs first).

but if you want to be sure that it always prints First Second Third in that order do following

replace // here with t.join();
replace // some with t1.join();

Output is FirstSecondThird // guaranteed always

join is join thread that executes this code (main here) to end and so when you say t.join() Thread t will complete first and only than main will start.

now first t thread will run to completion printing First than t1 Seond and main thread runs Third will be printed last always.
[ September 12, 2005: Message edited by: Santana Iyer ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread synchronization question ?
 
Similar Threads
Another Thread Question
Thread lock doubt
Mock Question Doubt...EQ+
Enthuware toughest question doubt
synchronized run method.