views:

1573

answers:

7

I'm trying to implement a simple class like this:

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

When I try to include a print() method in myClass and use it, I get a "cannot find symbol" in class java.lang.Thread. I don't really have to make this a thread, but I would like to, just to test it. Will I have to change it if I want my print() method to work?

EDIT: I am sorry, I just realized I can call print() inside the run() function lol. Why can't I call it outside though? That doesn't make sense to me. If I add synchronized or something can I call the function outside of run/the class?

EDIT2: Sorry I miswrote the names here.

EDIT3: I'm currently doing this:

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

If I use new Runner, it seems I can't use start() and join(). Is there a way to go about that?

EDIT4: Okay, let's try one more time please: I have myEnvironment, which is a class and I have myAgent, which is another class. myAgent is the thread. myAgent requires a myEnvironment, so I was passing it as a parameter to the constructor. However, I couldn't do this by extending Thread, because constructor (myEnvironment) wasn't found. Do I have to set myEnvironment via another function or can I pass it using the constructor?

A: 

You can have additional methods.

Your problem seems to be the naming of your variables!!

Fortyrunner
A: 

Answer to your question is yes they can.

Rob Di Marco
+11  A: 

You can implement whatever methods you want. However, you need to make sure the reference uses your class name, and not 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!
Outlaw Programmer
Yup, can't declare an interface, you can only implement one! +1
windfinder
What do you mean "declare an interface?" Runnable r = new MyRunner(); is certainly valid but you can only call methods defined in Runnable, namely run().
Outlaw Programmer
A: 

Can you provide the code where you actually call print() and indicate the scope where print() is defined? Otherwise we would just have to guess what you code looks like. It is very unlikely that your problem has anything do with threads.

Peter Lawrey
Agreed. His example has almost zero relevence to the actual problem at hand. However, your post should probably be a comment and not an 'answer.'
Outlaw Programmer
A: 

I think your code in the sample is wrong. This line shouldn't compile if myClass doesn't extend Thread:

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

It probably is something like this, which is giving you errors because Thread doesn't have a print() method:

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

Instead, do something like this:

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) { }
}
Pesto
+4  A: 

You have to remember the different classes/interfaces you are using here:

  • Thread
  • Runnable
  • Your subclasses of Runnable or Thread

You can only call methods on a variable if the declared type of the variable has that method. And Runnable objects are not Threads; they are merely code which a Thread can run.

Example:

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();
Mr. Shiny and New
A: 

Hi, I faced similar problem, not really a problem but some misunderstanding.

somebody who was working on my code before me created some method and passed Runnable as parameter, more likely:

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

Then calling myMethod out of main looks like:

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

So, to supply parameter to myMethod I need to instantiate object of (in this case anonymous) class implementing Runnable.

My question is: is it necessary to use Runnable in this example? Can I use any different interface? I mean I can create new interface with single method i.e.

interface MyInterface{ void doThis(); }

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

And of course client too:

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

Or maybe something is about Runnable?!

klaudio