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();
}
}
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();
}
}