【问题标题】:thread and creating object线程和创建对象
【发布时间】:2012-10-15 21:38:44
【问题描述】:

我正在尝试了解线程并在互联网上找到一些示例。这是一个每 3 秒输出“hello, world”的 java 类。但是我有一种感觉,关于创建 Runable 对象的部分是多余的。

而不是写

Runnable r = new Runnable(){ public void run(){...some actions...}}; 

我可以将方法run() 放在其他地方以便于阅读吗?

这就是我所拥有的:

public class TickTock extends Thread {
    public static void main (String[] arg){
        Runnable r = new Runnable(){
            public void run(){
                try{
                    while (true) {
                        Thread.sleep(3000);
                        System.out.println("Hello, world!");
                    }
                } catch (InterruptedException iex) {
                    System.err.println("Message printer interrupted");
                }
            }
        };
      Thread thr = new Thread(r);
      thr.start();
}

这就是我想要完成的事情

public static void main (String[] arg){ 
          Runnable r = new Runnable() //so no run() method here, 
                                  //but where should I put run()
          Thread thr = new Thread(r);
          thr.start();
    }

【问题讨论】:

  • 除了下面的实际答案,考虑学习如何使用 IDE 的重构工具来做这样的事情(将匿名类变成内部类或“普通”非嵌套类)。

标签: java multithreading redundancy


【解决方案1】:

我可以将方法 run() 放在其他地方以便于阅读吗?

是的,您可以像这样创建自己的可运行文件

public class CustomRunnable implements Runnable{
// put run here
}

然后

Runnable r = new CustomRunnable () ;
Thread thr = new Thread(r);

【讨论】:

    【解决方案2】:

    Java threads tutorial,您可以使用稍微不同的样式:

    public class HelloRunnable implements Runnable {
    
        public void run() {
            System.out.println("Hello from a thread!");
        }
    
        public static void main(String args[]) {
            (new Thread(new HelloRunnable())).start();
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      只需将您的匿名 Runnable 类设为内部静态类,如下所示:

      public class TickTock {
      
          public static void main (String[] arg){
              Thread thr = new Thread(new MyRunnable());
              thr.start();
          }
      
          private static class MyRunnable implements Runnable {
      
              public void run(){
                  try{
                      while (true) {
                          Thread.sleep(3000);
                          System.out.println("Hello, world!");
                      }
                  } catch (InterruptedException iex) {
                      System.err.println("Message printer interrupted");
                  }
              }
          }
      }
      

      或者由于TickTock 已经在您的示例代码中扩展了Thread,您可以重写它的run 方法:

      public class TickTock extends Thread {
      
          public static void main (String[] arg){
              Thread thr = new TickTock();
              thr.start();
          }
      
          @Override
          public void run(){
              try{
                  while (true) {
                      Thread.sleep(3000);
                      System.out.println("Hello, world!");
                  }
              } catch (InterruptedException iex) {
                  System.err.println("Message printer interrupted");
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-10
        • 2013-08-07
        • 2015-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多