0
Executors.newFixedThreadPool(NO_OF_THREADS, new ThreadFactory() {
 @Override
  public Thread newThread(Runnable r) {
   return new Thread(() -> {
   });
  }
}).execute(//returned thread here);

How to pass the anonymous thread as an argument to the execute function? Creating a new instance of the returned thread object to each thread.

raghu
  • 95
  • 1
  • 10
  • [This SO post may help](https://stackoverflow.com/questions/5107158/how-to-pass-parameters-to-anonymous-class) – gtgaxiola Dec 27 '17 at 18:10
  • 3
    Your question is unclear (at least for me). What do you mean by "anonymous thread"? There are anonymous classes which extend/implement other types so it would suggest something like `new Thread(..){...}` but I don't see anything like this in your example. What are you trying to do here? What happens instead? For now your question looks like [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Pshemo Dec 27 '17 at 18:11
  • for `execute()` method I need to pass the thread returned by the `newThread()` – raghu Dec 27 '17 at 18:14
  • 1
    No, you don't need to do any of that. You can always invoke `Thread.currentThread()` to get current thread instance. – M. Prokhorov Dec 27 '17 at 18:15
  • Why do you need to use a `ThreadFactory`? Can't you just pass your `Runnable` to execute? – Jorn Vernee Dec 27 '17 at 18:17
  • 1
    solved what? What did you want? You did not use parameter `Runnable r`, so your thread pool would not work as expected. – Alexei Kaigorodov Dec 27 '17 at 18:23
  • @AlexeiKaigorodov yes only one instance of the thread is started, Not as many specified by NO_OF_THREADS. Sorry for the haste. – raghu Dec 27 '17 at 18:30
  • How about overriding execute method in anonymous thread? – Amit Mahajan Dec 27 '17 at 20:28
  • 3
    This doesn't make any sense. Why are you passing an empty runnable? You're supposed to return a thread that executes `r`. – shmosel Dec 27 '17 at 21:58

0 Answers0