2019年8月26日 星期一

[Java程式]_Ch14.2_執行緒生命週期



歡迎訂閱頻道:杜仲教學YouTube
https://www.youtube.com/channel/UCElwt6s8Ea_V-gnu9sZZT5g?sub_confirmation=1
杜仲教學部落格:
https://derekcctu.blogspot.com/

1 則留言:

  1. class Test implements Runnable{
    String id;
    Test(String name){
    id=name;
    }
    @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();
    }
    Thread t=Thread.currentThread();
    System.out.println(id+" is running..timer="+timer+"當前執行緒:"+t.getName());
    }
    }
    }
    public class A14 {
    public static void main(String[] args) {
    Test dog=new Test("dog");
    Test cat=new Test("cat");
    Thread t1=new Thread(dog,"1.狗執行緒");
    Thread t2=new Thread(cat,"2.貓執行緒");
    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+"="+a/b);
    System.out.println("主程式結束...");
    }
    }

    /*
    class Test extends Thread{
    String id;
    Test(String name){
    id=name;
    }
    public void run(){
    for(int i=1;i<=4;i++){
    for(int j=1;j<999999999;j++);
    System.out.println(id+" is running..");
    }
    }
    }
    */

    回覆刪除

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...