2013-06-18 42 views
0

在C#中管理线程,我们可以按照如下使用线程池

System.Threading.Thread thd1 = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork)); 
    thd1.Start(); 
    System.Threading.Thread thd2 = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork)); 
    thd2.Start(); 
    thd1.Join(); 
    thd2.Join(); 

我们如何管理线程使用类似的线程池,而无需使用上面的语句创建线程?

+1

之一它是一个C#相关的问题? –

+0

是的。它已经在c#中完成了。它可能有许多程序并行运行;所以我想使用线程池,而不是管理单个线程,如果可能的话。 –

+0

因此,请使用ThreadPool类,然后使用QueueUserWorkItem方法。或者更高效地使用Task或BackgroundWorker类。否则很不清楚你想要“管理”什么。 –

回答

0

在Java中的许多选项使用ThreadPoolExecutor的,这只是其中

private ExecutorService executorService = Executors.newCachedThreadPool(); 
... 
executorService.execute(new Runnable() { 
    @Override 
    public void run() { 
    ... 
    } 
});