【问题标题】:Can classes in java that implement runnable have methods other than run()?java中实现runnable的类可以有run()以外的方法吗?
【发布时间】:2009-01-30 20:43:57
【问题描述】:

我正在尝试实现一个像这样的简单类:

public static void main(String args[])
{
    try
    {
        myClass test = new Thread(new myClass(stuff));
        test.start();
        test.join();
    }
    catch (Throwable t) { }
}

当我尝试在 myClass 中包含 print() 方法并使用它时,我在 java.lang.Thread 类中得到“找不到符号”。我真的不必把它变成一个线程,但我想,只是为了测试它。如果我想让我的 print() 方法工作,我是否必须更改它?

编辑:对不起,我刚刚意识到我可以在 run() 函数中调用 print(),哈哈。为什么我不能在外面打电话呢?这对我来说没有意义。如果我添加同步或其他东西,我可以在运行/类之外调用该函数吗?

EDIT2:对不起,我在这里写错了名字。

EDIT3:我目前正在这样做:

Thread test = new Thread(new myClass(stuff));
teste.start();
teste.join();

如果我使用 new Runner,我似乎无法使用 start() 和 join()。有办法解决吗?

EDIT4:好的,让我们再试一次: 我有 myEnvironment,它是一个类,我有 myAgent,它是另一个类。 myAgent 是线程。 myAgent 需要一个 myEnvironment,所以我将它作为参数传递给构造函数。但是,我无法通过扩展 Thread 来做到这一点,因为没有找到构造函数 (myEnvironment)。我必须通过另一个函数设置 myEnvironment 还是可以使用构造函数传递它?

【问题讨论】:

  • 我稍微整理了一下你的例子。我可以原谅以小写字母开头的类名,但让我们确保至少可以编译!

标签: java multithreading


【解决方案1】:

你可以实现任何你想要的方法。但是,您需要确保引用使用您的类名,而不是 Runnable:

public class MyRunner implements Runnable
{
    @Override public void run();
    public void somethingElse();
}

Runnable r = new MyRunner();
r.somethingElse(); // won't work, this method not defined by Runnable.

MyRunner m = new MyRunner();
m.somethingElse(); // success!

【讨论】:

  • 是的,不能声明一个接口,你只能实现一个! +1
  • “声明一个接口”是什么意思?可运行 r = new MyRunner();当然是有效的,但你只能调用 Runnable 中定义的方法,即 run()。
【解决方案2】:

你必须记住你在这里使用的不同类/接口:

  • 线程
  • 可运行
  • Runnable 或 Thread 的子类

如果变量的声明类型具有该方法,则只能调用该变量的方法。并且 Runnable 对象不是线程;它们只是线程可以运行的代码。

例子:

class MyRunnable implements Runnable() {
  public void run() { /* do run stuff */ }
  public void print() { /* do print stuff */ }
}

class MyThread extends Thread() {
  public void run() { /* do run stuff */ }
  public void print() { /* do print stuff */ }
}

Thread t = new Thread(new MyRunnable());
t.start();
t.print(); // error, t is a Thread not a MyRunnable and not a MyThread
t.join();

MyRunnable mr = new MyRunnable();
mr.run(); // doesn't run in its own thread
mr.print(); // this is ok

Runnable r = new MyRunnable();
r.run(); // doesn't run in its own thread
r.print(); // error, r is defined as Runnable which has no print() method

MyThread mt = new MyThread();
mt.start();
mt.print(); // this is ok because mt is a MyThread
mt.join();

【讨论】:

    【解决方案3】:

    您可以有其他方法。

    您的问题似乎是变量的命名!

    【讨论】:

      【解决方案4】:

      你的问题的答案是他们可以。

      【讨论】:

        【解决方案5】:

        您能否提供实际调用 print() 的代码并指出定义 print() 的范围?否则我们只需要猜测你的代码是什么样的。您的问题不太可能与线程有关。

        【讨论】:

        • 同意。他的例子与手头的实际问题的相关性几乎为零。但是,您的帖子可能应该是评论而不是“答案”。
        【解决方案6】:

        我认为您在示例中的代码是错误的。如果 myClass 不扩展 Thread,则此行不应编译:

        myClass test = new Thread(new myClass(stuff));
        

        它可能是这样的,它会给你错误,因为 Thread 没有 print() 方法:

        Thread test = new Thread(new myClass(stuff));
        

        相反,请执行以下操作:

        public static void main(String args[])
        {
            try
            {
                myClass foo = new myClass(stuff);
                myClass test = new Thread(foo);
                test.start();
                test.join();
                foo.print();
            }
            catch (Throwable t) { }
        }
        

        【讨论】:

          【解决方案7】:

          我也遇到过类似的问题,不是真正的问题,而是一些误解。

          在我之前处理我的代码的人创建了一些方法并将 Runnable 作为参数传递,更有可能:

          void myMethod(Runnable runnable){
          runnable.run();
          }
          

          然后从 main 中调用 myMethod 看起来像:

          public static void main(String args[])
          {
              try
              {
                  myMethod(new Runnable(){
                                public void run() {
                                    //do something...;
                          }});
              }
              catch (Throwable t) { }
          }
          

          因此,要为 myMethod 提供参数,我需要实例化实现 Runnable 的(在本例中为匿名)类的对象。

          我的问题是:在这个例子中是否有必要使用 Runnable ?我可以使用任何不同的界面吗?我的意思是我可以用单一方法创建新接口,即

          interface MyInterface{ 
          void doThis();
          }
          
          then change look of myMethod:
          void myMethod(MyInterface myObject){
          myObject.doThis();
          }
          

          当然还有客户:

          public static void main(String args[])
          {
              try
              {
                  myMethod(new MyInterface (){
                                public void doThis() {
                                    //do something...;
                          }});
              }
              catch (Throwable t) { }
          }
          

          或者也许是关于 Runnable 的东西?!

          【讨论】:

            猜你喜欢
            • 2014-03-11
            • 1970-01-01
            • 1970-01-01
            • 2021-09-05
            • 1970-01-01
            • 1970-01-01
            • 2011-11-28
            • 1970-01-01
            相关资源
            最近更新 更多