Java Executors.defaultThreadFactory() Example
November 15, 2023
defaultThreadFactory()
is the method of java.util.concurrent.Executors
that returns the object of ThreadFactory
.
ThreadFactory tf=Executors.defaultThreadFactory();
Executors.defaultThreadFactory()
, new thread is created as below.
Thread th=tf.newThread(ob);
Example
DefaultThreadFactoryTest.javapackage com.concretepage; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; public class DefaultThreadFactoryTest { public static void main(String... args){ ThreadFactory tf=Executors.defaultThreadFactory(); Thread th=tf.newThread(new DefaultThreadFactoryTest().new SampleThread() ); th.start(); } class SampleThread implements Runnable { @Override public void run() { int cnt = 0; for (; cnt < 5; cnt++) { System.out.println("run:" + cnt); } } } }