- Default thread factory java
- Uses of ThreadFactory in java.lang.ref
- Uses of ThreadFactory in java.nio.channels
- Uses of ThreadFactory in java.nio.channels.spi
- Uses of ThreadFactory in java.util.concurrent
- ThreadFactory Interface in Java with Examples
- Java
- Java
- Java
- Java
- Default thread factory java
- Uses of ThreadFactory in java.nio.channels
- Uses of ThreadFactory in java.nio.channels.spi
- Uses of ThreadFactory in java.util.concurrent
Default thread factory java
Provides reference-object classes, which support a limited degree of interaction with the garbage collector.
Defines channels, which represent connections to entities that are capable of performing I/O operations, such as files and sockets; defines selectors, for multiplexed, non-blocking I/O operations.
Uses of ThreadFactory in java.lang.ref
Uses of ThreadFactory in java.nio.channels
Uses of ThreadFactory in java.nio.channels.spi
Uses of ThreadFactory in java.util.concurrent
Returns a thread factory used to create new threads that have the same permissions as the current thread.
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available, and uses the provided ThreadFactory to create new threads when needed.
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed.
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create a new thread when needed.
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.
Creates a new ThreadPoolExecutor with the given initial parameters and default rejected execution handler.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
ThreadFactory Interface in Java with Examples
The ThreadFactory interface defined in the java.util.concurrent package is based on the factory design pattern. As its name suggests, it is used to create new threads on demand. Threads can be created in two ways:
1. Creating a class that extends the Thread class and then creating its objects.
Java
2. Creating a class that implements the Runnable interface and then using its object to create threads.
Java
However, ThreadFactory is another choice to create new threads. This interface provides a factory method that creates and returns new threads when called. This factory method takes a Runnable object as an argument and creates a new thread using it.
The Hierarchy of ThreadFactory
java.util.concurrent ↳ Interface ThreadFactory
Implementation of ThreadFactory interface
Since ThreadFactory is an interface, the factory method defined inside it has to be implemented first in order to be used. Here is the simplest implementation of the ThreadFactory interface :
Java
Now, we can create objects of the CustomThreadFactory class and use its newThread(Runnable command) method to create new threads on demand. In the above implementation, the newThread method just creates a new thread by calling the Thread constructor which takes a Runnable command as the parameter.
There are many classes(such as ScheduledThreadPoolExecutor , ThreadPoolExecutor etc.) that use thread factories to create new threads when needed. Those classes have constructors that accept a ThreadFactory as argument. If any custom ThreadFactory is not given then they use the default implementation of ThreadFactory interface.
The Executors class in java.util.concurrent package provides Executors.defaultThreadFactory() static method that returns a default implementation of ThreadFactory interface.
Example: Below example code demonstrates ThreadFactory interface.
Java
«Total number of threads created using CustomThreadFactory line number43 index42 alt2″> + threadFactory.getCount());
Command 1 executed Command 2 executed Command 4 executed Command 3 executed Command 5 executed Total number of threads created using CustomThreadFactory = 5
Why use ThreadFactory?
In the above example, the newThread(Runnable) factory method ultimately creates a new thread using the given Runnable command. Then why use ThreadFactory? We could directly create threads from the Runnable commands by calling the Thread constructor that we did in the newThread(Runnable) method. Here are some reasons,
- We can give the threads more meaningful custom names. It helps in analyzing their purposes and how they work.
- We can have the statistics about the created threads like the count of threads and other details. We can restrict the creation of new threads based on the statistics.
- We can set the daemon status of threads.
- We can set the thread priority.
- We can have all the features confined in one class.
Default Thread Factory
It is the default thread factory that is implemented by the Executors.defaultThreadFactory() static method. This default ThreadFactory is used by many classes (such as ScheduledThreadPoolExecutor, ThreadPoolExecutor etc.) when they are not given any custom ThreadFactory. Those classes create new threads using the default ThreadFactory. This default ThreadFactory creates all the new threads in the same ThreadGroup(A ThreadGroup represents a group of threads). All the created new threads are non-daemon with priority set to the smallest of Thread.NORM_PRIORITY and the maximum priority permitted in the ThreadGroup. The threads created by this default ThreadFactory are given names in the form of pool-N-thread-M (As examples, pool-1-thread-1, pool-1-thread-2, pool-2-thread-1 etc.) where N is the sequence number of this factory, and M is the sequence number of the threads created by this factory.
Example: The below example demonstrates how the default ThreadFactory can be used.
Default thread factory java
Defines channels, which represent connections to entities that are capable of performing I/O operations, such as files and sockets; defines selectors, for multiplexed, non-blocking I/O operations.
Uses of ThreadFactory in java.nio.channels
Modifier and Type | Method and Description |
---|---|
static AsynchronousChannelGroup | AsynchronousChannelGroup. withFixedThreadPool (int nThreads, ThreadFactory threadFactory) |
Uses of ThreadFactory in java.nio.channels.spi
Modifier and Type | Method and Description |
---|---|
abstract AsynchronousChannelGroup | AsynchronousChannelProvider. openAsynchronousChannelGroup (int nThreads, ThreadFactory threadFactory) |
Uses of ThreadFactory in java.util.concurrent
Returns a thread factory used to create new threads that have the same permissions as the current thread.
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available, and uses the provided ThreadFactory to create new threads when needed.
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed.
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create a new thread when needed.
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.
Creates a new ThreadPoolExecutor with the given initial parameters and default rejected execution handler.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.