aspose file tools*
The moose likes Threads and Synchronization and the fly likes Program on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Program on Threads" Watch "Program on Threads" New topic
Author

Program on Threads

Priya Madhuri
Greenhorn

Joined: Jan 18, 2001
Posts: 6
/*
Dear Friends,
I am appearing for SCJP and want appear for it next month.
I request your help to solve this problem and make me understanding Threads concepts.
Please modify the below program and send it to me.
Email: priya-madhuri@usa.net
-Priya
//I REQUIRE THE RESULT LIKE BELOW (Each Name should print continously 5 times)
Name= George
Name= George
Name= George
Name= George
Name= George
Name= Peter
Name= Peter
Name= Peter
Name= Peter
Name= Peter
Name= John
Name= John
Name= John
Name= John
Name= John
/*
// GETTING RESULT LIKE BELOW(Names are getting printed randomly, eventhogh I used synchronous methods)
Name= George
Name= George
Name= George
Name= George
Name= John
Name= Peter
Name= George
Name= John
Name= Peter
Name= John
Name= Peter
Name= John
Name= Peter
Name= John
Name= Peter
*/
// PROGRAM STARTS

// PROGRAM ENDS

------------------
Priya
Hi, Please use [ code] [ /code] tags to format your code. Thanks.
[This message has been edited by Rahul Mahindrakar (edited January 18, 2001).]


Priya
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
The problem is the synchronized Display() method synchronizes on this. this is a reference to MyThread. You have 3 different MyThread objects all created in the run method of the Thread1 class.
Synchronization locks objects, not methods or code. When the Display() method enters, it acquires three different locks, one for each different object. Therefore, there is no synchronization between the threads. Remember, synchronization in Java is not a mutex or a critical section as it is sometimes advertised. Synchronization does not mean that only one thread can execute. It means you have that object's lock. Another thread can execute the same code concurrently if it's synchronized on another instance of that object and thus a different lock.
To fix the code, you need to synchronize on the same object. One way to do this is to add a static object to sychronize on. Add this to your MyThread class:
private static byte[] lock = new byte[0];
Then in the Display method, wrap the for loop with a synchronized block:

I hope this helps,
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java


Senior Software Engineer, IBM
author of: Practical Java
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
I agree with Peter, you are creating 3 Threads and synchronization works on the instance of one thread at a time unless you synchronized at class level that I think isn�t the point of your question.
Here is my correction of your code:

Hope it helps
vineeta pillai
Greenhorn

Joined: Jan 18, 2001
Posts: 2
Priya,
you are calling the synchronized method Dispaly() on different objects.
Different objects are created because a new variable is declared
each time the run() method of Thread1 is called as the object is declared
inside the run() method.
Synchronisation means holding lock on the synchronised methods of a single
object.
Here Display() is called by the first, second and third method independently
on different objects each having their own lock.
That's why you are getting unexpected result
My version:

Hi, Please use [ code] [ /code] tags to format your code. Thanks

[This message has been edited by Rahul Mahindrakar (edited January 19, 2001).]
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
Vineeta:
Look that in your display method you forgot to put the loop statement, so you can�t test in a proper manner the results.
But another thing I notice is that you are creating 3 different objects like Priya did.
I agree with your concepts but the code doesn�t give the results we want.
Look at my example and try it.
Any comments or questions?
Marcela
Priya Madhuri
Greenhorn

Joined: Jan 18, 2001
Posts: 6
Dear friends,
With all of your help and Javaranch, I cleared SCJP
and secured 76%(I feel happy with this as I prepared One and Half month only).
I got 85% in Threads.
Thank you verymuch.
-Priya
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1851
Congrats Priya!!! . so any new plans or targets

------------------
Regds.
Mahindrakar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program on Threads