Hi everyone,
I have following code ::
case 1:
public class Resource {
public synchronized void met1()
{ }
public void met2() { }
}
And three threads t1,t2,t3.If thread t1 is using met1() can other
threads t2 ,t3 can use met2() at same time ??
case 2:
Also if the met1() is changed to
public static
synchronized void met1() { }
Can t2,t3 access met2() if t1 is using met1() at same time?
Ingo R. Homann - 05 Jun 2007 10:41 GMT
Hi,
> Hi everyone,
> I have following code ::
[quoted text clipped - 7 lines]
> And three threads t1,t2,t3.If thread t1 is using met1() can other
> threads t2 ,t3 can use met2() at same time ??
Yes.
> case 2:
> Also if the met1() is changed to
> public static
> synchronized void met1() { }
> Can t2,t3 access met2() if t1 is using met1() at same time?
Yes.
Please read a book!
Ciao,
Ingo
Tom Hawtin - 05 Jun 2007 11:39 GMT
> public class Resource {
> public synchronized void met1()
[quoted text clipped - 3 lines]
> And three threads t1,t2,t3.If thread t1 is using met1() can other
> threads t2 ,t3 can use met2() at same time ??
The key concept is that
synchronized void fn() {
...
}
is just a short form of
void fn() {
synchronized (this) {
...
}
}
You just need to keep your eye on which instance you are synchronising on.
Tom Hawtin