1. 程式人生 > >Java執行緒入門二

Java執行緒入門二

1. currentThread方法

public class MyThread extends Thread {

    public MyThread() {
        System.out.println("MyThread.MyThread: "+MyThread.currentThread().getName());
    }

    @Override
    public void run() {
        
        System.out.println("run: "+ MyThread.currentThread().getName());
    }
} public class Demo01 { public static void main(String[] args) { MyThread run = new MyThread(); run.start(); } }

輸出結果如下:
MyThread.MyThread: main --> run物件建立是main執行緒
run: Thread-0 --> run方法執行是自己啟動的執行緒

public class CountOperate extends Thread {

    public CountOperate
() { System.out.println("CountOperate -------- begin"); System.out.println("name: "+Thread.currentThread().getName()); System.out.println("this.getName():" + this.getName()); System.out.println("CountOperate -------- end"); } @Override public void run()
{ System.out.println("run -------- begin"); System.out.println("threadName: "+Thread.currentThread().getName()); System.out.println("this.getName():" + this.getName()); System.out.println("run -------- end"); } } public static void main(String[] args) throws InterruptedException { CountOperate run = new CountOperate(); Thread thread = new Thread(run); thread.setName("a"); thread.start(); /*run.setName("b"); run.start();*/ }

輸出結果如下:
CountOperate -------- begin
name: main
this.getName():Thread-0
CountOperate -------- end
run -------- begin
threadName: a
this.getName():Thread-0
run -------- end

改變main方法程式碼如下:

public static void main(String[] args) {

	CountOperate run = new CountOperate();
	/*Thread thread = new Thread(run);
	thread.setName("a");
	thread.start();*/
	run.setName("b");
	run.start();
}

輸出結果如下:
CountOperate -------- begin
name: main
this.getName():Thread-0
CountOperate -------- end
run -------- begin
threadName: b
this.getName():b
run -------- end

造成兩個結果不一樣的原因:Thread.currentThread()和this的區別

2. isAlive

public class MyThread extends Thread {
    @Override
    public void run() {

        System.out.println("run: "+ this.isAlive());
    }
}
public static void main(String[] args) {

	MyThread run = new MyThread();
	//MyThread t = new MyThread();
	//Thread run = new Thread(t);
	System.out.println(run.isAlive());//false
	run.setName("b");
	run.start();
	System.out.println(run.isAlive());//true run還沒有執行完畢
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(run.isAlive());//false 
}

輸出結果如下:
false --> 執行緒尚未啟動
true --> run執行緒還沒有執行完畢
run: true --> 執行MyThread執行緒裡面run方法
false --> run執行緒在1s內執行完畢了

修改main方法程式碼如下:

public static void main(String[] args) {

	//MyThread run = new MyThread();
	MyThread t = new MyThread();
	Thread run = new Thread(t);
	System.out.println(run.isAlive());//false
	run.setName("b");
	run.start();
	System.out.println(run.isAlive());//true run還沒有執行完畢
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(run.isAlive());//false run在1s內執行完畢了
}

輸出結果如下:

false
true
run: false 和之前的唯一區別:Thread.currentThread()和this的區別
false

更復雜的示例:

public class CountOperate extends Thread {

    public CountOperate() {
        System.out.println("CountOperate -------- begin");
        System.out.println("thread: " + Thread.currentThread().getName());//thread: main
        System.out.println("thread-isAlive: " + Thread.currentThread().isAlive());//thread-isAlive: true
        System.out.println("this.getName():" + this.getName());//this.getName():Thread-0
        System.out.println("this.isAlive:" + this.isAlive());//this.isAlive:false
        System.out.println("CountOperate -------- end");
    }

    @Override
    public void run() {

        System.out.println("run -------- begin");
        System.out.println("thread: " + Thread.currentThread().getName());//thread: b
        System.out.println("thread-isAlive: " + Thread.currentThread().isAlive());//thread-isAlive: true
        System.out.println("this.getName():" + this.getName());//this.getName():Thread-0
        System.out.println("this.isAlive:" + this.isAlive());//this.isAlive:false
        System.out.println("run -------- end");
    }
}
public static void main(String[] args) {

	//CountOperate run = new CountOperate();
	CountOperate t = new CountOperate();
	Thread run = new Thread(t);
	System.out.println(run.isAlive());//false
	run.setName("b");
	run.start();
	System.out.println(run.isAlive());//true run還沒有執行完畢
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(run.isAlive());//false run在1s內執行完畢了
}

輸出結果如下:
CountOperate -------- begin
thread: main
thread-isAlive: true
this.getName():Thread-0
this.isAlive:false
CountOperate -------- end
false
true
run -------- begin
thread: b
thread-isAlive: true
this.getName():Thread-0
this.isAlive:false
run -------- end
false

修改main方法程式碼如下:

public static void main(String[] args) {

	CountOperate run = new CountOperate();
	//CountOperate t = new CountOperate();
	//Thread run = new Thread(t);
	System.out.println(run.isAlive());//false
	run.setName("b");
	run.start();
	System.out.println(run.isAlive());//true run還沒有執行完畢
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(run.isAlive());//false run在1s內執行完畢了
}

輸出結果如下:
CountOperate -------- begin
thread: main
thread-isAlive: true
this.getName():Thread-0
this.isAlive:false
CountOperate -------- end
false
true
run -------- begin
thread: b
thread-isAlive: true
this.getName():b
this.isAlive:true
run -------- end
false

3. sleep() 略去

在指定的毫秒數內讓"當前正在執行的執行緒" 休眠(暫停執行)
當前正在執行的執行緒:是指 this.currentThread返回的執行緒

4. getId: 獲取執行緒的唯一標識 略去