程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> API解讀:Thread

API解讀:Thread

編輯:JAVA編程入門知識

  線程是一個和平台關系比較密切的概念,這裡我們也不能看出它的具體實現,只能看一下它的表現了.

  public class Thread implements Runnable

  public final static int MIN_PRIORITY = 1;
  public final static int NORM_PRIORITY = 5;
  public final static int MAX_PRIORITY = 10;
  //以上三個是表示線程的優先級的,默認的都是和父線程具有相同的優先級
  public static native Thread currentThread();
  //獲得當前運行線程的線程實例,注重這是個靜態方法
  public static native void yield();
  //當前線程主動放棄執行,讓其他線程可以運行,這個是雷鋒精神
  public static native void sleep(long millis) throws InterruptedException;
  //當前線程自己休眠一會兒,過了這段時間後重新回到可執行狀態,有時候為了等別人就自己先睡一會兒
  //不過這一覺睡多長時間是比較難確定的
  public static void sleep(long millis, int nanos) throws InterruptedException
  //這個方法有點多余,把後面的納秒四捨五入而已,最終的效果或者是sleep(millis)和sleep(millis+1).

  構造函數
  public Thread();
  public Thread(Runnable target)//這個是最常用的構造函數
  public Thread(ThreadGroup group, Runnable target)//不是很關心輸入哪個線程組
  public Thread(String name) //我們似乎不關心線程的名字
  public Thread(ThreadGroup group, String name)
  public Thread(Runnable target, String name)
  public Thread(ThreadGroup group, Runnable target, String name)
  public Thread(ThreadGroup group, Runnable target, String name,long stackSize)
  public synchronized native void start();
  //運行線程用start方法,不知道裡面怎麼實現了,不過會調用run方法
  public void run() {
  if (target != null) {
  target.run();
  }
  }
  可以看出,假如沒有target參數,這個線程什麼也不做.

  //下面幾個方法已經不用了,我也不是很清楚為什麼,也不用去管這個了.
  public final void stop()
  public final synchronized void stop(Throwable obj)
  public final void suspend()
  public final void resume()

  
  public void interrupt()
  //中斷這個線程,這個方法除了設置中斷狀態位,不知道還做了什麼

  public static boolean interrupted() {
  return currentThread().isInterrupted(true);
  }
  //是否被中斷,重置中斷狀態
  public boolean isInterrupted() {
  return isInterrupted(false);
  }
  //是否被中斷
  private native boolean isInterrupted(boolean ClearInterrupted);
  
  public void destroy() {
  throw new NoSUChMethodError();
  }
  public final native boolean isAlive();
  //線程是否還活著,出生了但還沒死

  
  public final void setPriority(int newPriority)
  public final void setName(String name)
  public final String getName()
  public final ThreadGroup getThreadGroup()
  public static int activeCount() //獲得當前線程組獲得線程數
  public static int enumerate(Thread tarray[]) //將活動線程拷貝到一個數組
  public native int countStackFrames();
  public final synchronized void join(long millis) throws InterruptedException//等待這個線程死,這個家伙沒有耐心,過了一段時間就不等了.
  public final synchronized void join(long millis, int nanos) throws InterruptedException //沒什麼用的方法
   public final void join() throws InterruptedException //這個家伙很有耐心,為了拿遺產,一直等著他老爸死才肯罷休.
  
  public final void setDaemon(boolean on)
  public final boolean isDaemon()
  public final void checkAccess() //這個似乎不應該public
  public String toString()
  
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved