zoukankan      html  css  js  c++  java
  • 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类型的对象作为参数。

  • 相关阅读:
    前端工程化浅学
    jQuery学习
    黄金票据和白银票据获取域控权限
    [转]0day零距离
    [转]走近0day
    [转]人生如解 -- 关于破解组织的乱弹
    [转]WAREZ无形帝国
    [转]BSD系统正在死亡?一些安全研究人员这样认为
    Solaris:你好奇的十件事
    Windows和Office激活汇总
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3444582.html
Copyright © 2011-2022 走看看