Java Multithreading (Executorservice)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.