您现在的位置是:首页 >

Java2入门经典教程 11.1 了解线程[3]

火烧 2022-06-29 21:17:05 1045
Java2入门经典教程 11.1 了解线程[3] 试试看 派生Thread的子类 用一个例子我们可以看到这是如何工作的 我们将定义一个由Thread的类派生的类TryThread 执行从mai ()方

Java2入门经典教程 11.1 了解线程[3]  

Java2入门经典教程 11.1 了解线程[3]

    试试看 派生Thread的子类

    用一个例子我们可以看到这是如何工作的 我们将定义一个由Thread的类派生的类TryThread 执行从main()方法开始

import java io TOException;public java TryThread extends thread {private string firstName;                    //store for first nameprivate string secondName;                  //store for second nameprivate long awhile                        //Delay in millisecondspublic TryThread(string firsName string secondName long delay{this firstName=firstName                  //store the first namethis secondName=secondName;              //store the second nameawhile=delay                            //store the delaysetDaemon(true)                        //Thread is daemon}public static void main (string[]args){//create three threads Thread first =new TryThread( Hopalong cassidy L);Thread second=new TryThread( Marilyn Monroe L);Thread third =new TryThread( slim pickens L);system out println( press Enter when you have had enough n );first start();                           //start the first thread second start();                         //start the second threadthrid start();              //start the thrid threadtry{  system in read();        //wait until Enter key pressed  system out println(Enter prosed n); } catch (IOException e)    //Hand Io exception  {    system out println(e);      //Output the exception  }  system out println(Ending main());  return;  //Method where thread execption will stare  public void run()  {    try     {      while(ture)              //Loop indefinitely        {      system out prin(firstName);     //output first name     sleep(awhile);                   //wait awhile mses     system out print(secondName + n);  //output second name   }  }  catch(InteruptedException e)           //Hand thread interruption  {    system out println(firstName+secondName+e);     //Output the exception   }  }}

lishixinzhi/Article/program/Java/gj/201311/27755  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码