• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. This is my first post although I have been sucking information from this site for quite a while now. Can anyone tell me why the following program prints the numbers 0-9 in a random order regardless of whether or not the run() method is synchronized. As (I thought) I understood it, synchronized will only give one Thread object access to the method at a time. I would think that if I used "synchronized" the numbers 0-9 would be printed out in order 4 times. Thanks in advance for your help

public class tosh extends Thread
{
static Thread t1=new tosh();
static Thread t2=new tosh();
static Thread t3=new tosh();
static Thread t4=new tosh();
public static void main(String [] args)
{

t1.start();
t2.start();
t3.start();
t4.start();
}
public synchronized void run()
{
for (i=0;i<10;i++)
{
System.out.println(i);}
}
}
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Santosh
Keep in mind that in the code you posted the synchronized keyword will synchronize the method on the current(calling) object. In other words once t1 is in the run method no other code that is synchronized on t1 can execute until the run method exits.
However, in your example you are creating 4 different objects each of which have their run method synchronized on themselves. So the run methods do get locked but the other objects are not calling the run method with the same object they are calling the run methods on themselves.
Here is you code modified to show you how to go about locking the run method - you ahve to do it with a common object that can be used by each of the new threads you create.

hope that helps
[ March 26, 2002: Message edited by: Dave Vick ]
 
Santosh Bapat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave. That makes sense.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Santosh , Your code does not compile . please initialize int i before for loop.
 
reply
    Bookmark Topic Watch Topic
  • New Topic