class MyThread extends Thread{
private String data;
public void run(){
synchronized(data){
for(int i = 0; i<10; i++){
try{
Thread.sleep((int)(Math.random()*1000) );
}catch(Exception e){}
System.out.println(data);
}
}
}
}
public class TestMyThread {
public static void main(String args[]){
Thread t1 = new MyThread();
Thread t2 = new MyThread();
t1.setName(“hello”);
t2.setName(“world”);
t1.start();
t2.start();
}
}
这题运行报错的原因是啥? |
|