class thr2
{
	public static void main(String args[])
	{
		A a = new A("first","hello1");
		A a1= new A("second","hello2");


		try{
			a.t.join();
			a1.t.join();
		}catch(Exception e)
		{

		}
	}
}

class A  implements Runnable
{
	Thread t;
	String str;
	B b = new B();

	A(String name,String str)
	{
		t=new Thread(this,name);
		t.setName(name);
		this.str=str;
		t.start();
	}

	public void run()
	{
		b.sum(str);
	}


}

class B
{
    synchronized void sum(String str)
	{
		System.out.print("[" + str);
		try
		{
			Thread.sleep(100);
		}catch(Exception e)
		{

		}

		System.out.println("]");
	}
}

when synchronized with sum() is used then output is same when it is not there. tell me y it is not working here?

post the whole error message from the compiler

there is not any error! it is completely running. but when i use synchronised then output must be like this [hello1] [hello2] nut it is not coming like that. it is exactly same as when synchronised is not there. you can also check

Did you post your whole code? Are the classes in separate java files? wheres the public class?

Edited 4 Years Ago by zeroliken: n/a

Are the classes in separate java files? wheres the public class?

no. it is one .java file. i have not made any class as public. is it neccessary to do that ??

What output do you get; [hello1[hello2]] ?
A synchronised method like yours is synchronised on the current object (instance of the class). Because you have two different instances of B each invocation of sum is synchronised on a different object, so the synchronisation has no effect. If you synchronise them both on the same object you will get the expected output.

What output do you get; [hello1[hello2]] ?
A synchronised method like yours is synchronised on the current object (instance of the class). Because you have two different instances of B each invocation of sum is synchronised on a different object, so the synchronisation has no effect. If you synchronise them both on the same object you will get the expected output.

no. there is only one object of B (b here). where are two according to u ?

No, you have two instances of A, each of which creates its own instance of B, so that's two B's.


If you had
static B b = new B();
then there would only be one b.

Edited 4 Years Ago by JamesCherrill: n/a

No, you have two instances of A, each of which creates its own instance of B, so that's two B's.


If you had
static B b = new B();
then there would only be one b.

ohh ya ya! that's right! hey! how do u answer every question so easily ? it's damn impressive!

Been there before, usually. Pls mark this "solved" if you're happy.

please one more question! will u exactly tell me use of wait() and notify()? i have read many many examples but i m not getting it. please help if u can. will be thankful to u

sorry man, I'm not going to try to write a tutorial here, even if I had the time. There are loads on the web already, most of them better than I could do anyway. Like these:
http://www.java-samples.com/showtutorial.php?tutorialid=306
http://www.avajava.com/tutorials/lessons/how-do-i-use-the-wait-and-notify-methods.html
Read them carefully then, if you have specific questions, start a new thread for it.
OK?
J

ok ok! i will start a new thread as u said if i have for wait(). thanks for this thread. this is solved by you.

I was checking other threads then came back here only to find that JamesCherrill found the solution so simply:-O
Took me a while to see that...Just goes to show the difference in experience

@OP
sorry wasn't much help:$

This question has already been answered. Start a new discussion instead.