2013-12-10 20 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虚拟机退出。