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


Java Executor privilegedThreadFactory()用法及代碼示例


Executors 類的 privilegedThreadFactory() 方法返回一個線程工廠,用於創建與當前線程具有相同權限的新線程。該工廠使用與 defaultThreadFactory() 相同的設置創建線程,另外將新線程的 AccessControlContext 和 contextClassLoader 設置為與調用此特權線程工廠方法的線程相同。

用法

public static ThreadFactory privilegedThreadFactory()

參數

沒有傳遞參數。

返回

一個線程工廠

拋出

訪問控製異常

例子1

//import statement
  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.ThreadFactory;  
      
    public class ExecutorsprivilegedThreadFactoryExample1  {  
       
        public static void main(String[] args) {  
            ThreadFactory thrdfctry = Executors.defaultThreadFactory();  
            Thread t = thrdfctry.newThread(new Mythrd3());  
            t.start();        
        }   
    }  
      
    class Mythrd3 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 ExecutorsprivilegedThreadFactoryExample2  {  
       
        public static void main(String[] args) {  
            ThreadFactory thrdfctry = Executors.privilegedThreadFactory();
            for(int i=0 ; i<=5 ; i++){
            Thread t = thrdfctry.newThread(new Mythrd5());  
             t.start();        
        }   
    }  
    }
    class Mythrd5 implements Runnable {  
       
        @Override  
        public void run() {  
            System.out.println("Running thread concurrently ::" +  new Date());  
        }  
    }

輸出:

Running thread concurrently ::Sun Mar 17 03:24:04 IST 2019
Running thread concurrently ::Sun Mar 17 03:24:04 IST 2019
Running thread concurrently ::Sun Mar 17 03:24:04 IST 2019
Running thread concurrently ::Sun Mar 17 03:24:04 IST 2019
Running thread concurrently ::Sun Mar 17 03:24:04 IST 2019
Running thread concurrently ::Sun Mar 17 03:24:04 IST 2019




相關用法


注:本文由純淨天空篩選整理自 Java Executor privilegedThreadFactory() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。