Ho una domanda da principiante. Ho questo codice:Thread.join() equivalente nell'esecutore
public class Main
{
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated method stub
IntHolder aHolder=new IntHolder();
aHolder.Number=0;
IncrementorThread A= new IncrementorThread(1, aHolder);
IncrementorThread B= new IncrementorThread(2, aHolder);
IncrementorThread C= new IncrementorThread(3, aHolder);
A.start();
B.start();
C.start();
A.join();
B.join();
C.join();
System.out.println("All threads completed...");
}
}
Quale attenderà il completamento di tutti i thread. Se uso Executors
come questo:
public class Main
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
IntHolder aHolder=new IntHolder();
aHolder.number=0;
IncrementalRunable A= new IncrementalRunable(1, aHolder);
IncrementalRunable B= new IncrementalRunable(2, aHolder);
IncrementalRunable C= new IncrementalRunable(3, aHolder);
ExecutorService exec = Executors.newFixedThreadPool(3);
exec.execute(A);
exec.execute(B);
exec.execute(C);
//Don't know what to do here
System.out.println("All threads completed...");
}
}
Come posso sospendere il thread principale per attendere tutti i thread nel l'esecutore per finire, vale a dire i "Tutte le discussioni hanno completato ..." dovrebbe essere stampato dopo il tutto il le discussioni hanno fatto il loro lavoro?
controllo questo: http://stackoverflow.com/questions/3269445/executorservice-how-to-wait-for-all-tasks-to-finish – giorashc
Future ha aa metodo di blocco di blocco –