我有這個創建線程的class
,但我想將class
轉換爲Service
,希望它不會在方向更改時被破壞。將創建線程的類轉換爲服務
這裏是類:
public class Getter {
private final String ip;
private final int amount, poolSize;
private Vector<Integer> results = new Vector<Integer>();
private final ExecutorService es;
private Collection<Future<?>> futures = new LinkedList<Future<?>>();
public Getter(String ip, int amount, int poolSize) {
this.ip = ip;
this.amount = amount;
this.poolSize = poolSize;
es = Executors.newFixedThreadPool(this.poolSize);
}
public boolean working() {
boolean work = false;
for (Future<?> future : futures) {
if (!future.isDone()) {
work = true;
}
}
return work;
}
public Vector<Integer> getResults() {
Collections.sort(results);
return results;
}
public int threads(){
return poolSize;
}
public void start() {
for (int i = 0; i <= amount; i++) {
futures.add(es.submit(new Get(ip)));
}
es.shutdown();
}
public void stop(){
for (Future<?> future : futures) {
future.cancel(true);
}
}
private class Get implements Runnable {
private String ip;
private Get(String ip) {
this.ip = ip;
}
public void run() {
try {
// network stuff
// adds result to results Vector.
} catch (Exception ex) {
}
}
}
}
所以這是class
可能轉化爲Service
所以它會在後臺運行,不管是什麼,一旦它開始了嗎?
任何想法將我的課程轉換爲服務有多複雜?我有一些關於服務的經驗,但沒有任何關於asynctask的經驗。哪兩個更好用,哪個最容易使用? – 2012-08-16 11:27:02