2015-10-16 125 views
-3

在我的在線分析系統中,我想爲每個用戶請求啓動一個新線程來啓動算法。另外,我想保留每個線程的id以更新算法的進度。我對線程不是很熟悉,只是幫助我。 在此先感謝在java中爲每個用戶請求啓動一個新線程

+2

顯示任何研究成果! –

+2

如果你不熟悉線程,你會錯誤的。你可以做的最好的事情是在嘗試做任何事情之前閱讀並學習線程。 – Kayaman

回答

0

這可以爲您提供有關如何使用線程池運行多個線程的基本想法。您可以從這裏開始,根據您的需要定義您自己的處理程序和池大小。您還可以將Handler定義爲可調用而不是可運行

public class MyTest { 

@Test 
public void myTest() { 
    ExecutorService es = Executors.newFixedThreadPool(5); 
    es.submit(new Handler(1)); 
    es.submit(new Handler(2)); 
    es.submit(new Handler(3)); 
    // block to show theads executions 
    try { 
     System.in.read(); 
    } catch (IOException e) {   
     e.printStackTrace(); 
    } 
} 

} 

class Handler implements Runnable { 
int userId; 
public Handler(int userId) { 
    super(); 
    this.userId = userId; 
} 
@Override 
public void run() { 
    System.out.println("running for user:"+userId); 

} 

} 
相關問題