new Thread(Runnable runnableObj) Vs. extends Thread
Possible Duplicate:
Java: “implements Runnable” vs. “extends Thread”
I'm just wondering is there some subtle difference between creating your own custom object that extends Thread and creating a thread using the Thread(Runnable) constructor?
I have some code that works fine when I use classes that extend Thread, but if I try to use logic that creates Threads by using the Thread(Runnable) constructor the new threads do not seem to work properly - I can't detect that they are alive in the same way as when I use the custom sub-classes I made and they do not seem to end, ever.
In my code I'm just spawning a few threads then searching through my list of threads to find one that is alive and joining with it until it dies. Then I once again search for a thread in the list that is alive and join to it.. this continues until all threads die.
Thanks for reading.
A Thread is a resource for doing work. A Runnable is a piece of work. Are you creating a new type of resource, or just defining the work you want done?
To 'end' the thread you simply return from the 'run' method. Would need to see an example of your code to see what you mean about not being able to tell if they're active.
Creating Runnables also of course makes it easier to refactor your code to use a ThreadPool in the future.
In my code I'm just spawning a few threads then searching through my list of threads to find one that is alive and joining with it until it dies.
How (and why) are you "searching through my list of threads"? I would instead add the Thread
to some sort of Collection
that you then can then iterate across and join with each Thread
in turn:
List<Thread> threads = new ArrayList<Thread();
for (int i = 0; i < 100; i++) {
Thread thread = new Thread(new Runnable() ...);
thread.start();
threads.add(thread);
}
...
for (Thread thread : threads) {
thread.join();
}
Even better would be to use one of the ExecutorService
s given by Executors
. They have a lot of features around creating thread-pools and waiting for the pool to completely finish. Almost always recommended over new Thread(...)
.
I can't detect that they are alive in the same way as when I use the custom sub-classes I made
If you are trying to set the name of the Thread
then you can't do this in the constructor of your Runnable
. You can however do this in the run()
method. You can do:
new Thread(new Runnable() {
public void run() {
Thread.currentThread().setName("...");
}
});
Otherwise, I'm not sure why you wouldn't be seeing your Thread
unless it has already finished.
they do not seem to end, ever.
Using a Runnable
, the thread finishes when the run()
method returns -- either through a return
or a throw
. This is very much the same as extending Thread
so I'm not sure why this wouldn't work.
I would really recommend to use the concurrency utility and not trying to manage yourself the Threads, which is complicated and error prone. Using the concurrent api will also help you to apply the right patterns straight away.
链接地址: http://www.djcxy.com/p/92074.html