使用Thread Class和Runnable接口输出

时间:2013-12-10 10:00:19

标签: java multithreading runnable

这两个程序都表示两者都是用相同的逻辑实现的,即在一个程序中,通过扩展Thread类来创建线程,另一个通过实现Runnable接口来创建线程。

这里的输出将是“打印一些值,直到我按下ENTER或RETURN键”,我的问题是,当我运行这两个程序“我们通过扩展Thread类创建Thread的程序将从扩展Thread类停止打印值直到我按ENTER / RETURN但是“我们说两者都在创建带有Runnable接口输出的线程的程序正在停止”

这两个程序的逻辑相同,唯一的区别是一个程序扩展Thread Class并实现Runnable接口不同。

通过扩展线程

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接口之间的区别。 他们中的大多数人更喜欢实现Runnable接口。但是从这个输出差异来看,我不知道哪个更喜欢

1 个答案:

答案 0 :(得分:1)

差异似乎与您的代码有关。你调用setDaemon(true) - JavaDoc - 在扩展Thread时,而不是在实现Runnable时。 JavaDoc说:当运行的唯一线程都是守护程序线程时,Java虚拟机退出。