简体   繁体   中英

Does instance of runnable class gets destroyed when Thread exits in Java

If have a class implementing runnable class with following code:

public class MyRunnable implements Runnable {
    public Thread t;
    // Other variables;

    public MyRunnable() {
        t = new Thread(this, "MyRunnable Thread");
        // Initialise other variables.
    }

    public void run() {
       //Do something.
    }
}

And i am making an instance of the above class in the following way:

public class MyFunc () {
  satic void main (String ards[]) {
     MyRunnable mr = new MyRunnable();
     mr.t.start();


     while (true) {
         Thread.sleep(10000);
         if (!mr.isAlive()) {
              //Execute mr again.
              // How to do it ?
         }
     }
  }
}

How should i do it?

I have two ways in mind, but not sure which one is correct:

1. mr.t.start();

2. MyRunnable mr = new MyRunnable(); mr.t.start();

Should i make a new instance of mr?

Or should i work with the existing instance or mr ?

Remove reference to Thread from MyRunnable .

Starting thread idiom in Java looks like this

new Thread(new MyRunnable()).start()

Normal rules of garbage collection applies to cleaning runnables. If no object references runnable it may be garbage collected.

There are several idioms around writing multi-threaded code in Java, see the Java tutorials . Keep it simple and separate:

public class YourTask implements Runnable {
   @Override
   public void run() {
      // do something
   }
}

A minimal example application:

public class YourApp {

public static void main(final String[] args) throws InterruptedException {

    final YourTask yourTask = new YourTask();
    final Thread thread = new Thread(yourTask);
    thread.start();

    thread.join();
   }
}

Be careful with concurrency - you shouldn't use this code in production until you have a proper understanding, for example by reading Java Concurrency in Practice .

I don't like this code.

Your Runnable shouldn't have a Thread member, public or private. I'd recommend removing it. Think simple: separation of concerns. This is what your Runnable ought to look like:

public class MyRunnable implements Runnable {
    public void run() {
       //Do something.
    }
}

That's it. Let other classes that know how to run things handle that part.

You're better off looking at the newer concurrent package classes, like Executor .

You shouldn't be trying to do a lot of multi-threaded programming unless you've read Brian Goetz' "Java Concurrency In Practice" and understood it thoroughly. You're less likely to run into trouble.

Runnable has the method run(), so you do not need separate Thread inside that.And nothing gets destroyed unless if you go out from the context of your variable (object) definition and you loose the reference.

http://www.javamex.com/tutorials/threads/thread_runnable_construction.shtml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM