manash
manash

Reputation: 7106

How to access Runnable during Thread creation inside ThreadFactory?

I have a ThreadPoolExecutor I've created with a custom ThreadFactory. When I call the execute() method passing my Runnable, a new Thread is created using my custom ThreadFactory. The problem is I can't find a way to access information inside the Runnable.

For example, I have a class Job that implements Runnable. Inside Job, I have a property A that I would like to access in the newThread() method of my custom ThreadFactory. The problem is that I can't cast the Runnable passed as parameter to Job since it's not a Job instance, it is an instance of the ThreadPoolExecutor.Worker class, and this class is private.

Upvotes: 0

Views: 319

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340743

You are either misusing or misunderstanding what thread pool does. The process of creating threads and submitting/running tasks is completely decoupled. Thread pool decides when create a new thread and it might not be related to any new task submitted at all.

And finally remember that one thread is most likely reused across several Runnables.

Upvotes: 5

Related Questions