是的 我帮你改一下你就可以看到效果了 public class Test extends Thread{ public Test(){ } private int a = 2; public static void main(String args[]){ Test t1 = new Test(); Test t2 = new Test(); t1.start(); t2.start();
} public void run(){ runRun();
} public static synchronized void runRun() { for(int i = 0 ;i< 100 ; i++) System.out.println(Thread.currentThread().getName() + " is running."); } }
t2.run();//main线程直接调用run方法
你这里定义了两个对象,同步块中得到的是当前对象的锁,所以两个对象就谈不上同步了
根据你的说法,也就是 synchronized(this)对于t1.run();和t2.run()不会有影响了是吧.
我帮你改一下你就可以看到效果了
public class Test extends Thread{
public Test(){
}
private int a = 2;
public static void main(String args[]){
Test t1 = new Test();
Test t2 = new Test();
t1.start();
t2.start();
}
public void run(){
runRun();
}
public static synchronized void runRun() {
for(int i = 0 ;i< 100 ; i++)
System.out.println(Thread.currentThread().getName() + " is running.");
} }