Reputation: 38705
I am implement a thread pool for my application and I want to use newCachedThreadPool along with ThreadFactory, and I wonder what I wrote below is correct.
1.The idea of using newCachedThreadPool is that thread can be reusable, and that idle thread will be terminated appropriately like the jdk7 doc provided. However, when I wrote the code, I have some doubt since in newThread(Runnable r)
, I return new instance of MyThread
, and I also does MyThread myThread = new MyThread();
in main
. So please if an experienced developers can point out whether what I did below are correct or not, I would greatly appreciated. Thank you
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class ThreadFactoryTest{
public static void main(String[] args) {
ExecutorService cachedPool = Executors.newCachedThreadPool(new MyThreadFactory());
MyThread myThread = new MyThread();
cachedPool.submit(myThread);
}
}
class MyThreadFactory implements ThreadFactory{
@Override
public Thread newThread(Runnable r) {
return new MyThread(r);
}
}
class MyThread extends Thread {
public MyThread(Runnable r){
super(r);
}
public MyThread(){
super();
}
@Override
public void run() {
Thread t = Thread.currentThread();
if(t instanceof MyThread){
System.out.println("Thread is MyThread");
}
System.out.println("Inside: " + this.getName());
Utils.awesome();
}
}
class Utils{
public static void awesome(){
Thread t = Thread.currentThread();
if(t instanceof MyThread){
System.out.println("Inside awesome() and Thread is MyThread. Provide special awesomeness for MyThread.");
}
}
}
When I run the above program it produces
Thread is MyThread
Inside: Thread-1
Inside awesome() and Thread is MyThread. Provide special awesomeness for MyThread.
which are the correct outputs, what I am asking is whether I am setting up correctly.
Upvotes: 0
Views: 2851
Reputation: 54639
In general, you should not submit a Thread
to the ExecutorService#submit(Runnable)
method. Although Thread
implements the Runnable
interface, this is rather an artifact ( Why does Thread implement Runnable? ).
In general, you submit tasks to the ExecutorService
. And these tasks are descriptions about what has to be done, regardles of which thread will do it. The management of worker threads is completely left to the ExecutorService
implementation (and the ThreadFactory
that it is using internally).
At least one can say that
if(this instanceof MyThread){
System.out.println("Thread is MyThread");
}
does not make sense, because the instanceof
test will always be true. Maybe you wanted to test something like
if(Thread.currentThread() instanceof MyThread) {
System.out.println("This runnable is executed by a Thread that is a MyThread");
}
But since you're setting an appropriate ThreadFactory
, this message will always be printed
So depending on what your "awesomeness" should be, this could be done either in a special implementation of Runnable
, or in your special MyThread
class. More precisely: The appropriate solution will depend on whether this "awesome stuff" should be related to the tasks that are executed, or to the threads that are executing the tasks.
In any case, you should also consider to manually create an instance of ThreadPoolExecutor
where you override the beforeExecute or afterExecute method in order to do awesome things.
Upvotes: 1