2013-12-10 21 views
0

這兩個程序都表示兩個程序都使用相同的邏輯實現,即在一個程序中,通過擴展Thread類創建線程,並通過實現Runnable接口創建線程。使用線程類和可運行接口輸出

這裏的輸出將像「打印一些值,直到我按下ENTER或RETURN鍵」,我的問題是,當我運行這兩個程序「我們正在創建線程通過擴展線程類的程序將通過延長線程暫停直到我按下ENTER/RETURN但是「我們說的程序都在創建帶有Runnable接口輸出的線程正在停止」

這裏的邏輯在這兩個程序中是一樣的,唯一不同的是一個程序擴展了Thread Class和實現Runnable接口不同。

By Extending Thread

import java.io.IOException; 

    class TryThread extends Thread { 
     public TryThread(String firstName, String secondName, long delay) { 
     this.firstName = firstName; 
     this.secondName = secondName; 
     aWhile = delay; 
     setDaemon(true); 
     } 
     public void run() { 
     try { 
      while (true) { 
      System.out.print(firstName); 
      Thread.sleep(aWhile); 
      System.out.print(secondName + "\n"); 
      } 
     } catch (InterruptedException e) { 
      System.out.println(firstName + secondName + e); 
     } 
     } 
     private String firstName; 
     private String secondName; 
     private long aWhile; 
    } 
    public class Lab1 { 
     public static void main(String[] args) { 
     Thread first = new TryThread("A ", "a ", 200L); 
     Thread second = new TryThread("B ", "b ", 300L); 
     Thread third = new TryThread("C ", "c ", 500L); 
     System.out.println("Press Enter when you have had enough...\n"); 
     first.start(); 
     second.start(); 
     third.start(); 
     try { 
      System.in.read(); 
      System.out.println("Enter pressed...\n"); 
     } catch (IOException e) { 
      System.out.println(e); 
     } 
     return; 
     } 
    } 

輸出:

Press Enter when you have had enough... 

    A B C a  
    A b 
    B a  
    A c 
    C a  
    A b 
    B a  
    A b 
    B c 
    C a  
    A a  
    A b 
    B a  
    A c 
    C b 
    B a  
    A 
    Enter pressed... 

通過實現Runnable接口

import java.io.IOException; 

class TryThread1 implements Runnable { 
    public TryThread1(String firstName, String secondName, long delay) { 
    this.firstName = firstName; 
    this.secondName = secondName; 
    aWhile = delay; 
    } 
    public void run() { 
    try { 
     while (true) { 
     System.out.print(firstName); 
     Thread.sleep(aWhile); 
     System.out.print(secondName + "\n"); 
     } 
    } catch (InterruptedException e) { 
     System.out.println(firstName + secondName + e); 
    } 
     } 
    private String firstName; 

    private String secondName; 
    private long aWhile; 
} 
public class Lab2 { 
    public static void main(String[] args) { 
    Thread first = new Thread(new TryThread1("A ", "a ", 200L)); 
    Thread second = new Thread(new TryThread1("B ", "b ", 300L)); 
    Thread third = new Thread(new TryThread1("C ", "c ", 500L)); 
    System.out.println("Press Enter when you have had enough...\n"); 
    first.start(); 
    second.start(); 
    third.start(); 
    try { 
     System.in.read(); 
     System.out.println("Enter pressed...\n"); 
    } catch (IOException e) { 
     System.out.println(e); 
    } 
    return; 

    } 
} 

輸出

Press Enter when you have had enough... 

A B C a 
A b 
B a 
A c 
C a 
A b 
B a 
A b 
B c 
C a 
A a 
A b 
B a 
A c 
C b 
B a 
A a 
A b 
B c 
C a 
A 
Enter pressed... 

b 
B a 
A a 
A b 
B c 
C a 
A b 
B a 
A c 
C a 
A b 
B a 
A b 
B a 
A c 
C a 
A b 
B a 
A c 
C b 
B a 
A a 
A b 

請告訴我延長線和實現Runnable接口之間的差異。其中大多數人更喜歡實現可運行接口。但是從這個輸出差異來看,我不知道哪一個更喜歡

回答

1

這個區別似乎是特定於你的代碼的。您可以調用setDaemon(true) - JavaDoc - 擴展Thread時,但在執行Runnable時不適用。 JavaDoc說:當只有運行的線程都是守護進程線程時,Java虛擬機退出。