亚洲欧洲国产欧美一区精品,激情五月亚洲色五月,最新精品国偷自产在线婷婷,欧美婷婷丁香五月天社区

      考試首頁 | 考試用書 | 培訓課程 | 模擬考場 | 考試論壇  
      全國  |             |          |          |          |          |         
        當前位置:計算機等級 > 二級考試 > Java語言程序設計 > 考試輔導 > 文章內(nèi)容
        

      全國計算機等級考試Java語言程序設計輔導(102)

      中華IT學院   【 】  [ 2016年3月18日 ]

      下表列出了Thread類的一些重要方法:

      序號 方法描述
      1 public void start()
      使該線程開始執(zhí)行;Java 虛擬機調用該線程的 run 方法。
      2 public void run()
      如果該線程是使用獨立的 Runnable 運行對象構造的,則調用該 Runnable 對象的 run 方法;否則,該方法不執(zhí)行任何操作并返回。
      3 public final void setName(String name)
      改變線程名稱,使之與參數(shù) name 相同。
      4 public final void setPriority(int priority)
       更改線程的優(yōu)先級。
      5 public final void setDaemon(boolean on)
      將該線程標記為守護線程或用戶線程。
      6 public final void join(long millisec)
      等待該線程終止的時間最長為 millis 毫秒。
      7 public void interrupt()
      中斷線程。
      8 public final boolean isAlive()
      測試線程是否處于活動狀態(tài)。

      測試線程是否處于活動狀態(tài)。 上述方法是被Thread對象調用的。下面的方法是Thread類的靜態(tài)方法。

      序號 方法描述
      1 public static void yield()
      暫停當前正在執(zhí)行的線程對象,并執(zhí)行其他線程。
      2 public static void sleep(long millisec)
      在指定的毫秒數(shù)內(nèi)讓當前正在執(zhí)行的線程休眠(暫停執(zhí)行),此操作受到系統(tǒng)計時器和調度程序精度和準確性的影響。
      3 public static boolean holdsLock(Object x)
      當且僅當當前線程在指定的對象上保持監(jiān)視器鎖時,才返回 true。
      4 public static Thread currentThread()
      返回對當前正在執(zhí)行的線程對象的引用。
      5 public static void dumpStack()
      將當前線程的堆棧跟蹤打印至標準錯誤流。

      實例

      如下的ThreadClassDemo 程序演示了Thread類的一些方法:

      // 文件名 : DisplayMessage.java
      // 通過實現(xiàn) Runnable 接口創(chuàng)建線程
      public class DisplayMessage implements Runnable
      {
         private String message;
         public DisplayMessage(String message)
         {
            this.message = message;
         }
         public void run()
         {
            while(true)
            {
               System.out.println(message);
            }
         }
      }
      // 文件名 : GuessANumber.java
      // 通過繼承 Thread 類創(chuàng)建線程
      
      public class GuessANumber extends Thread
      {
         private int number;
         public GuessANumber(int number)
         {
            this.number = number;
         }
         public void run()
         {
            int counter = 0;
            int guess = 0;
            do
            {
                guess = (int) (Math.random() * 100 + 1);
                System.out.println(this.getName()
                             + " guesses " + guess);
                counter++;
            }while(guess != number);
            System.out.println("** Correct! " + this.getName()
                             + " in " + counter + " guesses.**");
         }
      }
      // 文件名 : ThreadClassDemo.java
      public class ThreadClassDemo
      {
         public static void main(String [] args)
         {
            Runnable hello = new DisplayMessage("Hello");
            Thread thread1 = new Thread(hello);
            thread1.setDaemon(true);
            thread1.setName("hello");
            System.out.println("Starting hello thread...");
            thread1.start();
           
            Runnable bye = new DisplayMessage("Goodbye");
            Thread thread2 = new Thread(bye);
            thread2.setPriority(Thread.MIN_PRIORITY);
            thread2.setDaemon(true);
            System.out.println("Starting goodbye thread...");
            thread2.start();
       
            System.out.println("Starting thread3...");
            Thread thread3 = new GuessANumber(27);
            thread3.start();
            try
            {
               thread3.join();
            }catch(InterruptedException e)
            {
               System.out.println("Thread interrupted.");
            }
            System.out.println("Starting thread4...");
            Thread thread4 = new GuessANumber(75);
           
                 thread4.start();
            System.out.println("main() is ending...");
         }
      }

      運行結果如下,每一次運行的結果都不一樣。

      Starting hello thread...
      Starting goodbye thread...
      Hello
      Hello
      Hello
      Hello
      Hello
      Hello
      Hello
      Hello
      Hello
      Thread-2 guesses 27
      Hello
      ** Correct! Thread-2 in 102 guesses.**
      Hello
      Starting thread4...
      Hello
      Hello
      ..........remaining result produced.
      分享到:
      本文糾錯】【告訴好友】【打印此文】【返回頂部
      將考試網(wǎng)添加到收藏夾 | 每次上網(wǎng)自動訪問考試網(wǎng) | 復制本頁地址,傳給QQ/MSN上的好友 | 申請鏈接 | 意見留言 TOP
      關于本站  網(wǎng)站聲明  廣告服務  聯(lián)系方式  站內(nèi)導航  考試論壇
      Copyright © 2007-2013 中華考試網(wǎng)(Examw.com) All Rights Reserved