3

I've one ThreadPoolExecuter to run multiple thread at the same time. in Runnable, I want to run method in another thread so, I've create another thread (thread A) in execute method, now I want to get result of thread A to run in executor thread. let me clarify with one sample:

ThreadPoolExecuter threadPool = Executors.newFixedThreadPool(5);

 threadPool.execute(new Runnable() {
            @Override
            public void run() {

                // do something in threadPool thread

                // call method in thread A
                getInfo(new QueryExecutorInterface() {
                    @Override
                    public void onPostExecute(Cursor cursor) {

                       // do other thing in threadPool thread again.

                    }
                });   
            }
        });

QueryExecutorInterface is my interface that I've want to pass to thread A and get result on ThreadPool thread. as I've call listener call back like following method i get result in thread A :

            class A extend Thread {

            @Override
            public void run() {

                // do something in thread A.

                queryExecutorInterface.onPostExecute(cursor);
            }
          }

PS: I can fix this scenario with using ReentrantLock class instead of using Thread A. but as I have one another layer above this I don't want to use locking.

1 Answers1

1

You just need to add another Runnable into the ThreadPool. So you have to make the ThreadPool final:

final ThreadPoolExecuter threadPool = Executors.newFixedThreadPool(5); // note the final modifier

threadPool.execute(new Runnable() {
    @Override
    public void run() {

        // do something in threadPool thread
        // call method in thread A
        getInfo(new QueryExecutorInterface() {
            @Override
            public void onPostExecute(Cursor cursor) {
                threadPool.execute(new Runnable() { // adding another callback so it runs in threadpool
                    @Override
                    public void run() {
                        // do the thing here
                    }
                });
            }
        });   
    }
 });
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
  • thanks for your reply. your solution has one problem but it's working. if I add 100 task to `threadPool`, after getting data, my new `runnable` move to end of the `threadPool queue`, is there any solution to run this `runnable `ASAP in `threadPool` queue? –  May 03 '16 at 08:37
  • You need some sort of locking then. But that will block the Threads in the pool until the responses come. I suggest you don't do that. On the other hand, you can have two separate thread pools. One for sending the requests and another to deal with the responses. – Tamas Rev May 03 '16 at 08:50
  • 1
    i think better way is implement `PriorityThreadPoolExecuter` like http://stackoverflow.com/questions/3545623/how-to-implement-priorityblockingqueue-with-threadpoolexecutor-and-custom-tasks. thanks for your answer –  May 03 '16 at 09:41