當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。