• Java基础之线程——使用Runnable接口(JumbleNames)


    控制台程序。

    除了定义Thread新的子类外,还可以在类中实现Runnable接口。您会发现这比从Thread类派生子类更方便,因为在实现Runnable接口时可以从不是Thread的类派生子类,并且仍然表示线程。Java只允许有单个基类,如果类派生于Thread,就不能再继承其他类中的功能。Runnabel接口只定义了方法run(),这个方法在启动线程时执行。

     1 import java.io.IOException;
     2 
     3 public class JumbleNames implements Runnable {
     4   // Constructor
     5   public JumbleNames(String firstName, String secondName, long delay) {
     6     this.firstName = firstName;                                        // Store the first name
     7     this.secondName = secondName;                                      // Store the second name
     8     aWhile = delay;                                                    // Store the delay
     9   }
    10 
    11   // Method where thread execution will start
    12   public void run() {
    13     try {
    14       while(true) {                                                    // Loop indefinitely...
    15         System.out.print(firstName);                                   // Output first name
    16         Thread.sleep(aWhile);                                          // Wait aWhile msec.
    17         System.out.print(secondName+"
    ");                             // Output second name
    18       }
    19     } catch(InterruptedException e) {                                  // Handle thread interruption
    20       System.out.println(firstName + secondName + e);                  // Output the exception
    21     }
    22   }
    23 
    24   public static void main(String[] args) {
    25     // Create three threads
    26     Thread first = new Thread(new JumbleNames("Hopalong ", "Cassidy ", 200L));
    27     Thread second = new Thread(new JumbleNames("Marilyn ", "Monroe ", 300L));
    28     Thread third = new Thread(new JumbleNames("Slim ", "Pickens ", 500L));
    29 
    30     // Set threads as daemon
    31     first.setDaemon(true);
    32     second.setDaemon(true);
    33     third.setDaemon(true);
    34     System.out.println("Press Enter when you have had enough...
    ");
    35     first.start();                                                     // Start the first thread
    36     second.start();                                                    // Start the second thread
    37     third.start();                                                     // Start the third thread
    38     try {
    39       System.in.read();                                                // Wait until Enter key pressed
    40       System.out.println("Enter pressed...
    ");
    41 
    42     } catch (IOException e) {                                          // Handle IO exception
    43       System.err.println(e);                                           // Output the exception
    44     }
    45     System.out.println("Ending main()");
    46     return;
    47   }
    48 
    49   private String firstName;                                            // Store for first name
    50   private String secondName;                                           // Store for second name
    51   private long aWhile;                                                 // Delay in milliseconds
    52 }

    不能在这个构造函数中调用setDaemon()方法,因为这个类并非派生于Thread类。
    在创建表示线程的对象之后并且在调用run()方法之前,需要调用setDaemon()方法。

    在main()方法中,仍然为每个执行线程创建了Thread对象,但这次使用的构造函数把Runnable类型的对象作为参数。

  • 相关阅读:
    C++课程学习笔记第六周:多态
    C++课程学习笔记第五周:继承
    C++课程学习笔记第四周:运算符的重载
    C++课程学习笔记第三周:类和对象提高
    C++课程学习笔记第一周:从C到C++
    C++课程学习笔记第二周:类和对象基础
    PyTorch的安装及学习资料
    PyTorch练手项目一:训练一个简单的线性回归
    PyTorch练手项目二:MNIST手写数字识别
    PyTorch练手项目四:孪生网络(Siamese Network)
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3444582.html
Copyright © 2020-2023  润新知