当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Executors defaultThreadFactory()用法及代码示例


Executors 类的 defaultThreadFactory() 方法返回一个用于创建新线程的默认线程工厂。这个工厂在同一个 ThreadGroup 中创建一个 Executor 使用的所有新线程。如果有一个 SecurityManager,它使用 System 组。

用法

public static ThreadFactory defaultThreadFactory()

参数

没有传递参数

返回

一个线程工厂

抛出

不适用。

例子1

import java.util.concurrent.Executors;  
    import java.util.concurrent.ThreadFactory;  
      
    public class ExecutordefaultThreadFactoryExample1 {  
       
        public static void main(String[] args) {  
            ThreadFactory thrdfctry = Executors.defaultThreadFactory();  
            Thread t = thrdfctry.newThread(new Mythrd());  
            t.start();        
        }   
    }  
      
    class Mythrd implements Runnable {  
       
        @Override  
        public void run() {  
            System.out.println("Running thread concurrently");  
        }  
    }

输出:

Running thread concurrently

例子2

import java.util.Date;
    import java.util.concurrent.Executors;  
    import java.util.concurrent.ThreadFactory;  
      
    public class ExecutordefaultThreadFactoryExample2 {  
       
        public static void main(String[] args) {  
            ThreadFactory thrdfctry = Executors.defaultThreadFactory(); 
            for(int i=0 ; i<=5 ; i++){
            Thread t = thrdfctry.newThread(new Mythrd2());  
             t.start();        
        }   
    }  
    }
    class Mythrd2 implements Runnable {  
       
        @Override  
        public void run() {  
            System.out.println("Running thread concurrently ::" +  new Date());  
        }  
    }

输出:

Running thread concurrently ::Sun Mar 17 02:34:38 IST 2019
Running thread concurrently ::Sun Mar 17 02:34:38 IST 2019
Running thread concurrently ::Sun Mar 17 02:34:38 IST 2019
Running thread concurrently ::Sun Mar 17 02:34:38 IST 2019
Running thread concurrently ::Sun Mar 17 02:34:38 IST 2019
Running thread concurrently ::Sun Mar 17 02:34:38 IST 2019




相关用法


注:本文由纯净天空筛选整理自 Java Executors defaultThreadFactory() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。