• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on syncrhonized methods  RSS feed

 
cornel jew
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have read several articles on threads, but i still can't figure out how threads work on synchronized methods/objects .
What i understand is when a thread enters a synch method the object is lock so that other threads can't acces that method.
But when you have two threads say a and b and a access a synch method with a counter in another class through its run method,b can also access that same method and the results are intermixed.
I don't know if i am doing it the right way.
Must you have two different thread classes to do it as in the Producer Consumer example or can you have just one thread class and create your objects and access other synch methods different classes.
I have post a question on the subject, but the answer was not satisfying.
Can you please light my candle with example code so that i can also see what you are seeing,cos my room is still dark and i can't see in here.
Here is the code.
class MyThread extends Thread
{
public MyThread(String msg)
{
super(msg);
}

public void run()
{
System.out.println(getName());
Independent in = new Independent();
in.test();
}
}
class Independent
{ synchronized void test()
{for(int i = 0;i<300;i++)
{
System.out.println(i);
}
}
public static void main(String[] args)
{
new MyThread("Thread one").start();
new MyThread("Thread two").start();
}
}
Thanks for your help
Cornel
[ December 14, 2002: Message edited by: cornel jew ]
 
Ramkumar Venkat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, you have created two objects of type Independence on each thread. When the first thread executes the test() method it acquires the lock of the object of type Independence created by it amd execute it. The second thread also follows the same procedure. So you will get a mixed output (output from both threads intermixed).
You have to change your code to
class MyThread extends Thread {
Test t;
public MyThread(String msg, Test t) {
super(msg);
this.t = t;
}
public void run() {
System.out.println(getName());
t.test();
}
}
public class Test {
synchronized void test() {
for(int i = 0;i<300;i++)
System.out.println(i);
}
public static void main(String[] args) {
Test t = new Test();
new MyThread("Thread one",t).start();
new MyThread("Thread two",t).start();

}
}
Hope this was the resulkt you were expecting.
 
cornel jew
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I now understand what i was doing wrong.I was actually creating two different Independant objects in my thread code,so each thread are accessing actually its own objects and thats why the output.After i have seen the modified code i realised what i was doing wrong.
Thanks for your advice.
Cornel
 
William Quantrill
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im still new to threads, but that first set of code in the original post seems strange.
Doesnt this run amuk and spiral out of control ? It seems that initially the main class that starts up, creates some threads that in turn create more instances of the main class and so on and so on...
does that code crash your computer ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!