天天看點

Java 多線程學習筆記3

今天簡單記錄一下 多線程裡面的幾個方法

1 currentThread currentThread 方法可以傳回目前代碼段正在被哪個線程調用資訊

package smaug.cloud.provider.thread.t2;

/**
 * Created by naonao on 17/12/9.
 */
public class MyThread implements Runnable {
    public MyThread() {
        System.out.println("目前線程名字" + Thread.currentThread().getName() + "  ");
    }

    @Override
    public void run() {
        System.out.println("目前線程名字" + Thread.currentThread().getName());
    }
}
           
package smaug.cloud.provider.thread.t2;

/**
 * Created by naonao on 17/12/9.
 */
public class MyTest {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread t = new Thread(myThread, "A");
        t.start();
    }
}
           

輸出

目前線程名字main  
目前線程名字A
           

但是如果代碼稍微改一下 就像下面這樣

public class MyTest {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread t = new Thread(myThread, "A");
        //t.start();
        t.run();
    }
}
輸出是啥nie
目前線程名字main  
目前線程名字main
           

再次驗證了run 方法隻是個普通的方法,如果要建立線程必須用start

在寫一個稍微複雜的例子

package smaug.cloud.provider.thread.t2;

/**
 * Created by naonao on 17/12/9.
 */
public class MyThread2 extends Thread {
    public MyThread2() {
        System.out.println("MyThread  begin --------");
        System.out.println("Thread.currentThread().getName() " + Thread.currentThread().getName());
        System.out.println("this.getName() " + this.getName());
        System.out.println("MyThread  end --------");
    }

    @Override
    public void run() {
        System.out.println("run  begin --------");
        System.out.println("Thread.currentThread().getName() " + Thread.currentThread().getName());
        System.out.println("this.getName() " + this.getName());
        System.out.println("run  end --------");
    }
}
           

輸出

MyThread  begin --------
Thread.currentThread().getName() main
this.getName() Thread-
MyThread  end --------
run  begin --------
Thread.currentThread().getName() bab
this.getName() bab
run  end --------
           

2.isAlive 判斷線程是否存活也就是是否還在運作

package smaug.cloud.provider.thread.t3;

/**
 * Created by naonao on 17/12/9.
 */
public class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " isAlive " +this.isAlive());
    }
}
           
package smaug.cloud.provider.thread.t3;

/**
 * Created by naonao on 17/12/9.
 */
public class MyTest {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        System.out.println("myThread is alive " + myThread.isAlive());
        myThread.start();
        System.out.println("myThread is alive " + myThread.isAlive());
    }
}
           

得到的輸出

myThread is alive false
Thread- isAlive true
myThread is alive false
           

3 sleep sleep方法是讓目前線程休眠一段時間

package smaug.cloud.provider.thread.t4;

/**
 * Created by naonao on 17/12/9.
 */
public class MyThread implements Runnable {
    @Override
    public void run() {
        try {
            System.out.println("begin " + System.currentTimeMillis());
            Thread.sleep();
            System.out.println("end   " + System.currentTimeMillis());
        } catch (InterruptedException e) {

        }
    }
}
           
package smaug.cloud.provider.thread.t4;

/**
 * Created by naonao on 17/12/9.
 */
public class MyTest {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread t = new Thread(myThread);
        t.start();
    }
}
           

從輸出時間來看,它睡着了兩秒鐘

begin 
end