0

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.

6
  • You never run your runnable in MyThread's run...
    – assylias
    Apr 2, 2014 at 8:18
  • @assylias: can u elaborate please? My threading knowledge is quite limited
    – Thang Pham
    Apr 2, 2014 at 8:25
  • You never call r.run()
    – assylias
    Apr 2, 2014 at 9:05
  • @assylias: I thought cachedPool.submit(myThread); does that, is it not?
    – Thang Pham
    Apr 2, 2014 at 9:09
  • No, Thread.run() does - but you never call super.run() here...
    – assylias
    Apr 2, 2014 at 9:10

1 Answer 1

1

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.

2
  • Thank you for your answer, the reason I check for t instanceof MyThread is because there are different types of thread. For example now, i just return MyThread(r), but what I want is to pull out some type of information of Runnable r, so that I can decide what thread to create, however, r is type ThreadPoolExecutor$Worker, so I dont know how. Do you have a solution for this Marco?
    – Thang Pham
    Apr 3, 2014 at 3:02
  • Yes, the Worker class does some internal bookkeeping. However, there is a pool of threads, and usually it should not matter which thread executes which task. Maybe you should describe on a higher abstraction level what you actually want to achieve...
    – Marco13
    Apr 3, 2014 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.