[Java]_ch14.2_執行緒的生命週期
package YPU;
class CTest implements Runnable{
String id;
public CTest(String id){
this.id=id;
}
@Override
public void run() {
for(int i=1;i<=4;i++){
int Timer=(int)(Math.random()*1000);
try {
Thread.sleep(Timer);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(id+" is running...Timer="+Timer+" 當前執行緒:"+Thread.currentThread().getName());
}
}
}
public class A14 {
public static void main(String[] args) {
CTest dog=new CTest("dog");
CTest cat=new CTest("cat");
Thread t1=new Thread(dog,"狗執行緒");
Thread t2=new Thread(cat,"貓執行緒");
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int a=155,b=9;
System.out.println(a+"/"+b+"="+((float)a/b));
System.out.println("主程式結束....");
}
}
/*
class CTest extends Thread{
String id;
public CTest(String id){
this.id=id;
}
public void run(){
for(int i=1;i<=4;i++){
for(int j=1;j<=900000000;j++);
System.out.println(id+" is running...");
}
}
}
public class A14 {
public static void main(String[] args) {
CTest dog=new CTest("dog");
CTest cat=new CTest("cat");
dog.start();
cat.start();
}
}
*/
沒有留言:
張貼留言