Thread Synchronization

S

sachin

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?
 
I

Ingo R. Homann

Hi,
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 ??
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
 
T

Tom Hawtin

sachin said:
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 ??

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top