File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Program on Threads

 
Priya Madhuri
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
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).]
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marcela Blei
Ranch Hand
Posts: 477
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 477
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1868
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congrats Priya!!! . so any new plans or targets

------------------
Regds.
Mahindrakar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic