• 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 urgent please

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the below given code synchronized has no effect since there are two different thread objects running independently
could someone show me how to make this synchronised effective
i seem to have got confused with threads at the last minute

class iocheck extends Thread
{
public synchronized void run()
{
for (int i= 1; i < 10; i++)
{
System.out.println(i + "hello");
}
}
public static void main(String args[])
{
new iocheck().start();
new iocheck().start();

}
}
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the problem might be that just declaring synchnized method is not enough to acquire the desired output.
when u have more than one synchronized methods, only one of them can run.
but here this is different case. you need to also synchronize with the objects created. Below might help understanding.it produces te required output
public synchronized void run()
{
for (int i= 1; i < 10; i++)
{
System.out.println(i + "hello");
}
}
public static void main(String args[])
{
test t1 = new test();
test t2 = new test();
synchronized(t1)
{
t1.start();
}
synchronized(t2)
{
t2.start();
}

}
}
hope it helps
regards
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello !
Keep cool.. don't get confused.
see this Maha Anna's explaination at- http://javaranch.com/maha/Discussions/Threads/threads.html
go to Discussions/Threads/Topic4(synchronised block)
All the best !
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, the problem is that the synchronized keyword on a method synchronizes that method for that object. When you say new iocheck().start(), you are creating an object. Since you do that twice, you end up with 2 objects running their own code. Make sense?
To make this code work without putting the synchronized blocks in like Kevin did, change your iocheck class to implement Runnable instead of extending Thread. Then in your main, try the following:
iocheck ioc = new iocheck();
new Thread(ioc).start();
new Thread(ioc).start();
The reason this works is because both Threads are trying to access the same run method of the same object, instead of 2 different ones (as in your code). So the second thread has to wait until the first thread releases the lock to that object's code. Then you will get the 123456789 123456789 output.
Basically, you're looking at the difference between a class lock and an object lock. A class lock operates on synchronized static blocks (including methods) in a class. An object lock operates on synchronized non-static methods.
Good luck on your exam!
April
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic