Executorservice exec = null;
list<ibeflightinfo> ibeflightinfo = new arraylist<ibeflightinfo> ();
transfervo[] b2gflights = new transfervo[]{};
try {
exec = Executors.newfixedthreadpool (2);
Callable Ibecall = new Ibegetflightthread (request);
Callable B2gcall = new B2ggetflightthread (Request.getdepairport (), Request.getarrairport (), request.getDepDate ());
/*
Submit (Callable)
Methods Submit (Callable) and method Submit (Runnable) are similar, but the difference is that they receive different parameter types. An instance of callable is similar to an instance of Runnable, but the call () method of callable can return a result. Method Runnable.run () cannot return the result.
The return value of the callable can be obtained from the future object returned by the method submit (callable).
*/
future<object> ibefuture = Exec.submit (Ibecall);
future<object> b2gfuture = Exec.submit (B2gcall);
int second = 0;
while (! Ibefuture.isdone () &&! B2gfuture.isdone ()) {
TimeUnit.MILLISECONDS.sleep (1000);
Second + = 1;
}
Ibeflightinfo = (list<ibeflightinfo>) ibefuture.get ();
B2gflights = (transfervo[]) b2gfuture.get ();
SYSTEM.OUT.PRINTLN ("Shared Time (seconds):" + second);
} catch (Exception e) {
E.printstacktrace ();
} finally {
/*
In order to close the thread in Executorservice, you need to call the shutdown () method. The Executorservice does not close immediately, but instead of receiving new tasks, all threads end up executing the current task, and the Executorservie really shuts down. All tasks that are committed to Executorservice before the shutdown () method is called will be executed.
If you want to close executorservice immediately, you can call the Shutdownnow () method. This method attempts to close all the tasks that are being performed immediately, and skips all tasks that have been committed but are not yet running. However, it is not guaranteed to be able to successfully shut down a task that is being performed, and it is possible that they have actually been shut down, and it is possible that it will be executed straight to the end of the task. This is one of the best attempts.
*/
Exec.shutdown ();
}
Java Multithreading (Executorservice)