synchronized(this)

T

tarik.karaoglu

Hi all,

Maybe you ve read this synchronization question a million time but
I will ask again.

At the below code, all 3 threads can be in atomic section and print
"A".
So what does lock on this stand for ? Is it lock on current running
thread so all 3
can acquire lock on itself ?

If synchronized(this) replaces with synchronized(strBuf), lock for
strBuf guarantees
only one access on same object and orders access to critical section.

Is this true or am i missing some point?

public class StringLock extends Thread {

static StringBuffer strBuf;
/** Creates a new instance of StringLock */
public StringLock(StringBuffer str) {
strBuf = str;
}

public void run(){
synchronized(this){
for(int i=0;i<100;i++){
System.out.print(Thread.currentThread().getName()+"
"+strBuf.toString());
try{
Thread.sleep(200);
}catch(Exception ex){
ex.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" deger
set ediliyor "+strBuf.toString());
char a = strBuf.charAt(0);
strBuf.setCharAt(0,++a);
System.out.println(Thread.currentThread().getName()+"
"+strBuf.toString());
System.out.println(Thread.currentThread ().getName()+"
deger set edildi "+strBuf.toString());
}
}

public static void main(String[] args){

StringBuffer strb2 = new StringBuffer("A");
char a = 'b';

System.out.println(a+" "+ ++a);

StringLock str = new StringLock(strb2);
str.setName("T1");
StringLock str2 = new StringLock(strb2);
str2.setName("T2");
StringLock str3 = new StringLock(strb2);
str3.setName("T3");

str.start();
str2.start();
str3.start();


}
}
 
R

Rogan Dawes

Hi all,

Maybe you ve read this synchronization question a million time but
I will ask again.

At the below code, all 3 threads can be in atomic section and print
"A".
So what does lock on this stand for ? Is it lock on current running
thread so all 3
can acquire lock on itself ?

If synchronized(this) replaces with synchronized(strBuf), lock for
strBuf guarantees
only one access on same object and orders access to critical section.

Is this true or am i missing some point?

public class StringLock extends Thread {

static StringBuffer strBuf;

Since you use a STATIC strBuf, there is only a single instance of it
shared between all instances of your StringLock class.

When you synchronize on it, all three threads are competing to lock a
single object.

When you synchronize on "this", this refers to the object itself, ie.
Object str effectively does:

synchronized (str) {}

Object str2 does:

synchronized (str2) {}

etc

In other words, they are all synchronizing on different objects, and
thus do not block each other.

Regards,

Rogan
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top