0

The definition of ThreadFactory interface as per documentation is An object that creates new threads on demand. Using thread factories removes hardwiring of calls to new Thread, enabling applications to use special thread subclasses, priorities, etc.

I have not been able to understand the term hardwiring of calls to new Thread , also I am not able to understand the proper use and purpose of ThreadFactory

Kindly please explain. Thanks.

Ravi Jain
  • 1,452
  • 2
  • 17
  • 41
  • 3
    This is a classic example of a [Factory Pattern](https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)). Nothing _to_ understand except that. – Boris the Spider May 25 '15 at 18:02
  • 1
    Refer this question [ThreadFactory usage in Java][1] it contains some helpful examples. [1]: http://stackoverflow.com/questions/3179733/threadfactory-usage-in-java – user2359597 May 25 '15 at 18:22

2 Answers2

3

Read about Dependency Injection and Inversion of Control. http://en.wikipedia.org/wiki/Dependency_injection

Suppose I write a library, and my library needs to create a thread. Suppose you have a large, complex application program, and you would like to use my library, but your application has strict policies about managing threads, and all of its threads must be created by calling a special application-specific module.

If my library calls Thread.new(), then you can't use it because it bypasses your policy, but if my library lets you give me a ThreadFactory, and my library uses your ThreadFactory to create its thread, then the problem is solved: You can give me a ThreadFactory that creates the thread by calling on your application's thread-management module.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
1

I have not been able to understand the term hardwiring of calls to new Thread

Basically you don't have to tightly couple caller with thread object instead you can deledate to ThreadFactory. One advantage of this i can think is lets say you want to mock the thread call for unit testing, you can achieve it easily

also I am not able to understand the proper use and purpose of ThreadFactory

See http://javahowto.blogspot.in/2011/11/why-use-threadfactory.html

M Sach
  • 33,416
  • 76
  • 221
  • 314