2016-12-26 23 views
1

我有使用線程的代碼,但我不使用java.util.concurrent的線程,所以我想更改我的代碼,但我有一些問題。 ?來自java.util.concurrent的線程

Thread thread = new Thread() { 
        public void run() { 
         doSomething(); 
        } 
       }; 

我想用執行這樣的主題,所以我該怎麼做,這是類似的方式java.util.concurrent中做到這一點,我試過了:

ExecutorService executorService = Executors.newCachedThreadPool(); 
Runnable runnable = new Runnable() { 
        public void run() { 
         //my code doing something 
        } 
       }; 

我也有:

List<Runnable> threadsList = new ArrayList<Runnable>(); 

,我有方法:

boolean isAllowedCreateNewThread(Runnable taskToRun){ 
for(int i = 0; i < threadsList.size(); i++){ 
     Runnable th = threadsList.get(i); 
     if(th.isAlive())doSomething(); //isAlive doesn't work since I have runnable instead of Thread 
boolean isAllowed = true ;//I have here some bool function but it doesn't matter 
    if(isAllowed){ 
     threadsList.add(taskToRun); 
     //service.submit(taskToRun); 
     executorService.execute(taskToRun); 
    } 
return isAllowed; 
} 

如果我有 List<Thread> threadsList = new ArrayList<Thread>();並且不使用ExecutorService並隨處更改Runnable to Thread it work's。 所以我認爲這也是我必須改變的,但是怎麼樣?什麼是關鍵字insted線程?可運行?或者是其他東西? 我也有:

for(int i = 0; i < threadsList.size(); i++){ 
     Thread th = threadsList.get(i); 
     if(th.isAlive())doSomething(); myThreadName.start(); 

我不得不改變isAlive()類似的java.util.concurrentmyThreadName.start(); 所以一般我想用Thread類使用從java.util.concurrent的

線程
+0

我已經讀了三次你的問題,但沒有明白你在問什麼。你究竟想實現什麼? – lexicore

+0

使用線程方法將代碼更改爲來自java.util.concurrent的方法 – grapexs

+0

因此,您已經嘗試了哪些內容以及遇到了哪些問題。 「爲我重寫我的代碼」不是一個好問題。 – lexicore

回答

1

您的代碼更改此代碼的東西可以用下面的代碼段替換你的代碼。

  1. 創建一個Runnable類。

    class MyRunable implements Runnable{ 
        public void run() { 
         doSomething(); 
        } 
        private void doSomething() { 
         System.out.println("Thread Executing is " + Thread.currentThread().getId()); 
        } 
    } 
    
  2. 創建一個具有所需線程數的執行程序服務。這創建了所需數量的線程池。

    int numberOfThreads = 4; // number of threads you want to create 
    ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); 
    
  3. 爲池中存在的每個線程創建Runnable並將其分配給執行程序。

    for(int i=0;i<numberOfThreads;i++){ 
        Runnable myRunable = new MyRunable(); 
        service.submit(myRunable); 
    } 
    
    service.shutdown(); // this is a must as not given ,it halts the termination of the program 
    

    樣本輸出

    Thread Executing is 9 
    Thread Executing is 10 
    Thread Executing is 8 
    Thread Executing is 11 
    

所以整個代碼如下:

class MyRunable implements Runnable { 
    public void run() { 
     doSomething(); 
    } 

    private void doSomething() { 
     System.out.println("Thread Executing is " + Thread.currentThread().getId()); 
    } 

    public static void main(String[] args) { 
     int numberOfThreads = 4; // number of threads you want to create 
     ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); 

     for(int i=0;i<numberOfThreads;i++){ 
      Runnable myRunable = new MyRunable(); 
      service.submit(myRunable); 
     } 

     service.shutdown(); 

    } 

} 
1

您可以使用執行人java.util.concurren噸。
執行者旨在創建和管理線程,因此您不必直接執行此操作。

在您的例子那就是:

ExecutorService executorService = Executors.newCachedThreadPool(); 
executorService.execute(() -> doSomething()); 


有許多類型的執行人,你必須決定使用哪一種考慮調用的上下文。
舉例來說,如果你想有隻有一個線程,你可以使用:

Executors.newSingleThreadExecutor(); 

線程固定數量的,即10:

Executors.newFixedThreadPool(10); 

或根據需要創建新主題:

Executors.newCachedThreadPool(); 


關於執行者的更多信息,您可以閱讀here
希望它有幫助。