1. 程式人生 > 實用技巧 >Java 執行緒2

Java 執行緒2

執行緒方法

  1、怎麼獲取當前執行緒物件?
    Thread t = Thread.currentThread();
    返回值t就是當前執行緒。

  2、獲取執行緒物件的名字
    String name = 執行緒物件.getName();

  3、修改執行緒物件的名字
    執行緒物件.setName("執行緒名字");

  4、方法休眠
    sleep(long millis)
    4.1、靜態方法:Thread.sleep(1000);
    4.2、引數是毫秒

  5、終斷t執行緒的睡眠

    interrupt();

package
com.bjpowernode.java.thread; /* 1、怎麼獲取當前執行緒物件? Thread t = Thread.currentThread(); 返回值t就是當前執行緒。 2、獲取執行緒物件的名字 String name = 執行緒物件.getName(); 3、修改執行緒物件的名字 執行緒物件.setName("執行緒名字"); 4、當執行緒沒有設定名字的時候,預設的名字有什麼規律?(瞭解一下) Thread-0 Thread-1 Thread-2 Thread-3 ..... */ public class
ThreadTest05 { public void doSome(){ // 這樣就不行了 //this.getName(); //super.getName(); // 但是這樣可以 String name = Thread.currentThread().getName(); System.out.println("------->" + name); } public static void main(String[] args) { ThreadTest05 tt
= new ThreadTest05(); tt.doSome(); //currentThread就是當前執行緒物件 // 這個程式碼出現在main方法當中,所以當前執行緒就是主執行緒。 Thread currentThread = Thread.currentThread(); System.out.println(currentThread.getName()); //main // 建立執行緒物件 MyThread2 t = new MyThread2(); // 設定執行緒的名字 t.setName("t1"); // 獲取執行緒的名字 String tName = t.getName(); System.out.println(tName); //Thread-0 MyThread2 t2 = new MyThread2(); t2.setName("t2"); System.out.println(t2.getName()); //Thread-1\ t2.start(); // 啟動執行緒 t.start(); } } class MyThread2 extends Thread { public void run(){ for(int i = 0; i < 100; i++){ // currentThread就是當前執行緒物件。當前執行緒是誰呢? // 當t1執行緒執行run方法,那麼這個當前執行緒就是t1 // 當t2執行緒執行run方法,那麼這個當前執行緒就是t2 Thread currentThread = Thread.currentThread(); System.out.println(currentThread.getName() + "-->" + i); //System.out.println(super.getName() + "-->" + i); //System.out.println(this.getName() + "-->" + i); } } }

package com.bjpowernode.java.thread;
/*
關於執行緒的sleep方法:
    static void sleep(long millis)
    1、靜態方法:Thread.sleep(1000);
    2、引數是毫秒
    3、作用:讓當前執行緒進入休眠,進入“阻塞狀態”,放棄佔有CPU時間片,讓給其它執行緒使用。
        這行程式碼出現在A執行緒中,A執行緒就會進入休眠。
        這行程式碼出現在B執行緒中,B執行緒就會進入休眠。
    4、Thread.sleep()方法,可以做到這種效果:
        間隔特定的時間,去執行一段特定的程式碼,每隔多久執行一次。
 */
public class ThreadTest06 {
    public static void main(String[] args) {

        // 讓當前執行緒進入休眠,睡眠5秒
        // 當前執行緒是主執行緒!!!
        /*try {
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        // 5秒之後執行這裡的程式碼
        //System.out.println("hello world!");

        for(int i = 0; i < 10; i++){
            System.out.println(Thread.currentThread().getName() + "--->" + i);

            // 睡眠1秒
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package com.bjpowernode.java.thread;
/*
關於Thread.sleep()方法的一個面試題:
 */
public class ThreadTest07 {
    public static void main(String[] args) {
        // 建立執行緒物件
        Thread t = new MyThread3();
        t.setName("t");
        t.start();

        // 呼叫sleep方法
        try {
            // 問題:這行程式碼會讓執行緒t進入休眠狀態嗎?
            t.sleep(1000 * 5); // 在執行的時候還是會轉換成:Thread.sleep(1000 * 5);
                                     // 這行程式碼的作用是:讓當前執行緒進入休眠,也就是說main執行緒進入休眠。
                                     // 這樣程式碼出現在main方法中,main執行緒睡眠。
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 5秒之後這裡才會執行。
        System.out.println("hello World!");
    }
}

class MyThread3 extends Thread {
    public void run(){
        for(int i = 0; i < 10000; i++){
            System.out.println(Thread.currentThread().getName() + "--->" + i);
        }
    }
}
package com.bjpowernode.java.thread;
/*
sleep睡眠太久了,如果希望半道上醒來,你應該怎麼辦?也就是說怎麼叫醒一個正在睡眠的執行緒??
    注意:這個不是終斷執行緒的執行,是終止執行緒的睡眠。
 */
public class ThreadTest08 {
    public static void main(String[] args) {
        Thread t = new Thread(new MyRunnable2());
        t.setName("t");
        t.start();

        // 希望5秒之後,t執行緒醒來(5秒之後主執行緒手裡的活兒幹完了。)
        try {
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 終斷t執行緒的睡眠(這種終斷睡眠的方式依靠了java的異常處理機制。)
        t.interrupt(); // 干擾,一盆冷水過去!
    }
}

class MyRunnable2 implements Runnable {

    // 重點:run()當中的異常不能throws,只能try catch
    // 因為run()方法在父類中沒有丟擲任何異常,子類不能比父類丟擲更多的異常。
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "---> begin");
        try {
            // 睡眠1年
            Thread.sleep(1000 * 60 * 60 * 24 * 365);
        } catch (InterruptedException e) {
            // 列印異常資訊
            //e.printStackTrace();
        }
        //1年之後才會執行這裡
        System.out.println(Thread.currentThread().getName() + "---> end");

        // 呼叫doOther
        //doOther();
    }

    // 其它方法可以throws
    /*public void doOther() throws Exception{

    }*/
}