我有使用線程的代碼,但我不使用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.concurrent
和myThreadName.start();
所以一般我想用Thread類使用從java.util.concurrent的
我已經讀了三次你的問題,但沒有明白你在問什麼。你究竟想實現什麼? – lexicore
使用線程方法將代碼更改爲來自java.util.concurrent的方法 – grapexs
因此,您已經嘗試了哪些內容以及遇到了哪些問題。 「爲我重寫我的代碼」不是一個好問題。 – lexicore