我想在服務中創建一個具有3個線程的固定池的執行程序。線程在服務中啓動線程創建
這3個線程將根據我的SQLite數據庫做一些工作。
有沒有辦法告訴線程「呼」的服務,它會告訴他一些方法,「一個線程完成,你現在可以拉從數據庫中的數據,並啓動一個新線程」
這樣我可以操縱數據庫,接下來的線程將會相應地執行。
所有我能夠做到的是,填補了隊列所有我有我的數據庫中的數據,這樣,它不會反應在DB的變化,因爲我已經退出所有的數據
編輯:一些
public class MyThread implements Runnable {
private File _file;
private Context context;
private DBHelper helper;
private int requestId;
public MyThread(File file, Context context, int requestId) {
this._file = file;
this.context = context;
this.requestId = requestId;
}
@Override
public void run() {
// Thread work here
helper.deleteRequest(requestId);// remove from db to prevent infinite loop
// THIS IS THE QUESTION
MediaDownloadService.startNewThread();// ??? can it be done
} catch (IOException e) {
Log.e("Callable try", post.toString());
}
}
當然,我不希望它是靜態的
,是否有任何其他的方式來做到這一點:爲更好地理解
public class MediaDownloadService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
executor = new ThreadPoolExecutor(2,3,3000,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
//initiale start of new threads (first run)
}
public void startNewThread(){
if(helper.requestsExists()){
Map<Integer,String> requestMap = helper.getRequestsToExcute(0);
Set<Integer> keySet = requestMap.keySet();
Iterator<Integer> iterator = keySet.iterator();
executor.submit(new MyThread(file, getApplicationContext(), iteratorNext), 1);
}else{
executor.shutdown();
this.stopSelf();
}
}
和線程自己的代碼?
編輯以更好地瞭解一些代碼的問題,我想過將來objec像你提到的,但它的邏輯一直讓我失望,生病需要做一段時間(的Future.get()),並等待,直到它返回答案? –