繁体   English   中英

具有线程类和可运行接口的输出

[英]Output with Thread Class and Runnable interface

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

这里的输出就像“打印一些值,直到我按ENTER或RETURN键”,我的问题是,当我运行这两个程序时,“我们通过扩展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...  

通过实现可运行接口

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

区别似乎是特定于您的代码的。 你叫setDaemon(真) - 的JavaDoc -延长时Thread但不执行时Runnable JavaDoc说: 当所有正在运行的线程都是守护程序线程时,Java虚拟机退出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM