Java Executors.defaultThreadFactory() Example

By Arvind Rai, November 15, 2023
defaultThreadFactory() is the method of java.util.concurrent.Executors that returns the object of ThreadFactory.
ThreadFactory tf=Executors.defaultThreadFactory();
Using Executors.defaultThreadFactory(), new thread is created as below.
Thread th=tf.newThread(ob); 
All the threads created by this method, belongs to the same group.

Example

DefaultThreadFactoryTest.java
package 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);
            }
        }
    }
}
POSTED BY
ARVIND RAI
ARVIND RAI









©2023 concretepage.com | Privacy Policy | Contact Us