问题1:
线程运行的时候有必要判断是否为当前线程么?比如:
public class Clock extends Applet implements Runnable{
private Thread clockThread=null;
public void start(){ //Applet starting....
if(clockThread==null){
clockThread=new Thread(this,"Clock");
clockThread.start();
}
}
public void run(){
Thread myThread=Thread.currentThread();
while(clockThread==myThread){ //这里的这个判断有必要么?
repaint();
try{
Thread.sleep(1000); //这是要哪个线程sleep?
}
}
}
......
}问题2:
public class c1{
static{
.... //初始化
}
}
和
public class c1{
public c1(){
.... //初始化
}
}
有什么区别啊?
线程运行的时候有必要判断是否为当前线程么?比如:
public class Clock extends Applet implements Runnable{
private Thread clockThread=null;
public void start(){ //Applet starting....
if(clockThread==null){
clockThread=new Thread(this,"Clock");
clockThread.start();
}
}
public void run(){
Thread myThread=Thread.currentThread();
while(clockThread==myThread){ //这里的这个判断有必要么?
repaint();
try{
Thread.sleep(1000); //这是要哪个线程sleep?
}
}
}
......
}问题2:
public class c1{
static{
.... //初始化
}
}
和
public class c1{
public c1(){
.... //初始化
}
}
有什么区别啊?
...
} //静态初始化第一次创建对象会执行,以后都不再执行。
要不要判和程序有关
Thread.sleep(1000); 那个线程调用,那个线程sleep2
static block是类被load是被执行(不一定是非得创建实例)
而构造函数里当然只有new实例时才执行
而构造的属于实例