JAVA.Lang.Thread淺析

兩種方式實現生成一個線程

  1. 繼承Thread類,並覆蓋run方法,線程的工作方法就是run方法. 如下
    class MyThread extends Thread{
    public void run(){
    do….
    }
    }
    MyThread thread=new MyTread();
    thread.start();

  2. 實現Runbale接口,實現run方法
    class MyThread implements Runnable(){
    public void run(){
    do…
    }
    }
    Mythread thread = new MyThread();
    thread.start();

Thread類解析

public class Thread implements Runnable{
…} 從Thread類的定義上看其線程實現方式實質也是通過實現Runnable接口。

  1. 私有
    private char name[] 線程名
    private int priority 線程優先級
    private boolean daemon 是否是守護進程
    private boolean stillborn JVM狀態
    private Runnable target 將要運行的對象
    private ThreadGroup group 線程所在組
    private long statckSize 堆棧大小
    private long tid 線程ID
    private static long threadSeqNumber

  2. 公有
    public final static int MIN_PRIORITY = 1 最低優先級爲1
    public final static int NORM_PRIORITY=5 默認優先級爲5
    public final static int MAX_PRIORITY = 10 最大優先級爲10
    public static native Thread currentThread(); 返回當前線程對象
    public static native void sleep(long millis) 線程暫停millis毫秒
    public static void sleep(long millis int nanos) 線程暫停millis毫秒+nanos納秒
    public Thread() 實例化一個線程
    public Thread(ThreadGroup group, Runnable target) 實例化一個線程,指定所屬組
    public Thread(String name) 實例化一個線程,指定線程名
    public Thread (ThreadGroup group,String name) 實例化一個線程,指定所屬組和名字
    public Thread (Runnable target,String name) 實例化一個線程
    public sysnchronized void start();啓動線程
    public void run()
    public final void stop() 終止一個線程
    public void interrupt() 打斷一個線程
    public static boolean interrupted() 看線程是否被中斷
    pubilc boolean isInterrupted()
    public void destory() 銷燬線程
    public final native boolean isAlive() 看線程是否還存在
    public final void suspend() 線程掛起
    public final void resume() 喚醒一個唄掛起的線程
    public final void setPriority(int newPriority) 設置線程優先級
    public final sysnchronized void join(long millis) 等待millis之後死亡
    public final void checkAccess() 決定當前運行的線程是否可以修改此線程
    public getId() 獲取線程ID

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章