I am having issues with understanding synchronized blocks. I thought if a block was synchronized the
thread that was accessing it has control until it is finished with that block of code.
Why does it print:
first0
second0
first1
second1
first2
second2
instead of:
first0
first1
first2
second0
second1
second2
*********************************************
public class Holt extends Thread {
private
String sThreadName;
Holt(){}
Holt(String s){
sThreadName = s;
}
public String getThreadName(){
return sThreadName;
}
public void go(){
Holt first = new Holt("first");
first.start();
Holt second = new Holt("second");
second.start();
}
public static void main(String args[]){
Holt h = new Holt();
h.go();
}
public synchronized void run(){
for(int i=0; i < 3; i++){
System.out.println(getThreadName() + i);
}
}
}
**********************************************