Output with Thread Class and Runnable interface

The two programs say both are implemented with the same logic namely, in one program, creating thread by extending Thread class and the other by implementing Runnable interface.

Here the output will be like "Print some values till i press ENTER or RETURN key", My problem is, when i run these two programs "program where we are creating Thread by extending Thread class will by halted from extending Thread class printing values till i Press ENTER/RETURN but "program where we say both are creating Thread with Runnable interface output is getting halted"

Here the logic is same in these two programs, only difference is one program extending Thread Class and implementing Runnable interface different.

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;  
      }  
    }

Output :

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...  

By Implementing Runnable Interface

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;  

  }  
}  

Output

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   

Please tell me difference between extending thread and implementing Runnable interface. Most of them prefer Implementing Runnable Interface. But from this output differences, I don't know which one to prefer


The difference seems to be specific to your code. You call setDaemon(true) - JavaDoc - when extending Thread but not when implementing Runnable . The JavaDoc says: The Java Virtual Machine exits when the only threads running are all daemon threads.

链接地址: http://www.djcxy.com/p/92120.html

上一篇: 为什么Thread类实现Runnable接口

下一篇: 带有Thread Class和Runnable接口的输出