java中实现runnable的类是否可以使用run()以外的方法?

时间:2009-01-30 20:43:57

标签: java multithreading

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

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()函数lol中调用print()。为什么我不能在外面打电话呢?这对我来说没有意义。如果我添加synchronized或者某些东西,我可以在run / the class之外调用该函数吗?

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

EDIT3:我目前正在这样做:

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

如果我使用新的Runner,似乎我不能使用start()和join()。有没有办法解决这个问题?

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

7 个答案:

答案 0 :(得分:15)

您可以实现您想要的任何方法。但是,您需要确保引用使用您的类名,而不是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 :(得分:10)

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

  • 可运行
  • 您的Runnable或Thread的子类

如果声明的变量类型具有该方法,则只能调用变量上的方法。而Runnable对象不是Threads;它们只是一个Thread可以运行的代码。

示例:

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();

答案 2 :(得分:1)

您可以使用其他方法。

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

答案 3 :(得分:1)

回答你的问题是肯定的。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

我认为样本中的代码是错误的。如果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) { }
}

答案 6 :(得分:0)

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

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

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

然后从主要调用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的东西?!