class NewThread implements Runnable{ //在这里申明一个线成类
Thread t;
NewThread(){
t = new Thread(this,"DemoThread");
System.out.println("child Thread: "+t);
t.start(); // start the thread
} public void run(){ //线成真正开始执行的地方
try{
for(int i=5;i>0;i--){
System.out.println("Child Thread: "+i);
Thread.sleep(500); //线成挂起
}
} catch(InterruptedException e){
System.out.println("Child interrupted!");
}
System.out.println("Exit child thread.");
}
}class ThreadDemo {
public static void main(String[] args) {
new NewThread(); // create a new thread //产生一个线成
try{
for(int i=5;i>0;i--){
System.out.println("main Thread: "+i);
Thread.sleep(1000);
}
} catch(InterruptedException e){
System.out.println("Main interrupted.");
}
System.out.println("Exit main thread!");
}
}用new NewThread()产生一个线成时首先调用构造函数NewThread(){
t = new Thread(this,"DemoThread");
System.out.println("child Thread: "+t);
t.start(); // start the thread
在构造函数里面 真正产生一个线成 ,打应一句话 child Thread:DemoThread马上调用start()方法,start会自动调用run(),这里开始一个线成才算真正启动了
至于 Thread.sleep(500); 只是让出cpu的站用时间 可以让其他的thread也有启动的机会而已
Thread t;
NewThread(){
t = new Thread(this,"DemoThread");
System.out.println("child Thread: "+t);
t.start(); // start the thread
} public void run(){ //线成真正开始执行的地方
try{
for(int i=5;i>0;i--){
System.out.println("Child Thread: "+i);
Thread.sleep(500); //线成挂起
}
} catch(InterruptedException e){
System.out.println("Child interrupted!");
}
System.out.println("Exit child thread.");
}
}class ThreadDemo {
public static void main(String[] args) {
new NewThread(); // create a new thread //产生一个线成
try{
for(int i=5;i>0;i--){
System.out.println("main Thread: "+i);
Thread.sleep(1000);
}
} catch(InterruptedException e){
System.out.println("Main interrupted.");
}
System.out.println("Exit main thread!");
}
}用new NewThread()产生一个线成时首先调用构造函数NewThread(){
t = new Thread(this,"DemoThread");
System.out.println("child Thread: "+t);
t.start(); // start the thread
在构造函数里面 真正产生一个线成 ,打应一句话 child Thread:DemoThread马上调用start()方法,start会自动调用run(),这里开始一个线成才算真正启动了
至于 Thread.sleep(500); 只是让出cpu的站用时间 可以让其他的thread也有启动的机会而已
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货