Home  >  Core Java

Example of defaultThreadFactory in Java

By Arvind Rai, April 18, 2019
java.util.concurrent.Executors has a method that is defaultThreadFactory that returns the object of ThreadFactory.
ThreadFactory tf=Executors.defaultThreadFactory();
And then we create new Thread as
Thread th=tf.newThread(ob); 
We get the Thread object. All the threads created by this method belongs to the same group.
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
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us