codingenious
codingenious

Reputation: 8653

Do operation when thread dies

I am using ThreadPoolExecutor, and need to associate workerID with each thread, so when a thread dies, that ID should be removed and when new thread is created that ID should be used. But how do I ensure that the thread died. and perform something.

Can not rely on run method completion, as keepAliveTime is 15 minutes.

Is there any way to ensure this?

ThreadFactory newThread method is like this :

public Thread newThread(Runnable r)
{
    Thread thread = new Thread(group, r, (new StringBuilder()).append(namePrefix).append(threadNumber.getAndIncrement()).toString(), 0L);
    if(thread.isDaemon())
    {
        thread.setDaemon(false);
    }

    thread.setName((new StringBuilder()).append("Thread name : ").append(name).append(" WorkerId : ").append(getNextWorkerID());
    return thread;
}

getNextWorkerID returns newxtWorkerID.

Upvotes: 1

Views: 78

Answers (1)

Ray
Ray

Reputation: 3201

How about extending the ThreadPoolExecutor with an implementation that keeps a Map<Runnable, String> with the values being the workerIDs? You can override afterExecute(Runnable, Throwable) and remove the workerID from the Map in this method.

Upvotes: 1

Related Questions