Java2入门经典教程 11.1 了解线程[3]
Java2入门经典教程 11.1 了解线程[3]
![Java2入门经典教程 11.1 了解线程[3]](http://img.zhputi.com/uploads/c5cd/c5cd892fc31daa1d94df30a4608b8e6225866.jpg)
试试看 派生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