Can someone briefly explain on HOW and WHEN to use a ThreadFactory? An example with and without using ThreadFactory might be really helpful to understand the differences.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s assume we have some worker threads for different tasks and want them with special names (say for debugging purposes). So we could implement a ThreadFactory:
If you had such a requirement, it would be pretty difficult to implement it without a factory or builder pattern.
ThreadFactoryis part of the Java API because it is used by other classes too. So the example above shows why we should use ‘a factory to create Threads’ in some occasions but, of course, there is absolutely no need to implementjava.util.concurrent.ThreadFactoryto accomplish this task.