Java Multithreading: Getting Started (1)-Simple test Executeorservice

Source: Internet
Author: User

First, according to other articles, I have also experimented with the difference between runnable and callable:

1) The call () method of the callable interface has a return value, which makes it easy to handle the need to "process and return an object" (Runnable's Run () has no return value, but can be resolved within run () by means of a callback;

2) all use Executorservice to submit (), return Future<t>, but the former future.get () is empty, the latter can have content;

Test the Executors.newfixedthreadpool () with the following code:

 Public classtestexecutorshutdown{ Public Static voidMain (string[] args) throws Interruptedexception, executionexception{executorservice executor= Executors.newfixedthreadpool (Ten); Executor.submit (NewCallablea (executor)); }}classCallablea Implements Callable<object> {        classNewrunnable implements runnable{Private intID;  PublicNewrunnable (intID) {             This. ID =ID; } @Override Public voidrun () {System. out. println ("newrunnable:"+id+"executed; Thread hashcode:"+Thread.CurrentThread (). Hashcode ()); }} Executorservice Executor=NULL;  PublicCallablea (Executorservice executor) { This. Executor =executor; } @Override PublicObject Call () { for(intI=0; i< -; i++) {Executor.submit (Newnewrunnable (i)); }        return NULL; }}

Results:

Newrunnable:9 executed; Thread hashcode:777173972
Newrunnable:10 executed; Thread hashcode:777173972
Newrunnable:3 executed; Thread hashcode:622087230
Newrunnable:12 executed; Thread hashcode:622087230
Newrunnable:7 executed; Thread hashcode:348776228
Newrunnable:13 executed; Thread hashcode:622087230
Newrunnable:14 executed; Thread hashcode:622087230
Newrunnable:6 executed; Thread hashcode:948531615
Newrunnable:17 executed; Thread hashcode:948531615
Newrunnable:18 executed; Thread hashcode:948531615
Newrunnable:19 executed; Thread hashcode:948531615
Newrunnable:20 executed; Thread hashcode:948531615 ...

Only part of the result is posted here, and you can see that Newfixedthreadpool will split a runnable or callable task into a maximum of n (new defined) threads and do part of it, not repeat, but not necessarily in order.

In addition, I tried to insert the IF (i==50) {Executor.shutdownnow (), System.out.pringln (...) in the run () method, and/or shutdown ()}, as a result, the contents of other threads were printed out, Two questions are explained here:

1) The split of the task is the Submit () method (or Execute () method) is done at the beginning of the call, and then not in a particular order to start at the same time, this confirms the previous part of the conclusion, but also because of this, shutdown () and shutdownnow () the execution point if (i== 50) In fact, it is meaningless, because when the i==50 of the newrunnable thread is not necessarily, and even if executed, The System.out.println () method does not accurately represent the execution progress of all threads, it is possible that many threads have been turned on before Shutdownnow (), but it is only late to print to the console that the above results occur.

2) In addition, the document gives the explanation of the Shutdownnow () method is that "for a thread that has started executing, it is not necessarily completely shutdown", as the original text shows, this method to fully shutdown threads in the thread pool is not reliable:

shutdownnowlist<Runnable> for does that . There is no guarantees beyondbest-effort attempts to stop processing actively executing tasks. For example, typical implementations would cancel via Thread.Interrupt (), so any task that fails to respond to interrupts M Ay never terminate.

Write very poor Ha, step by step simple understanding of the role of the executor thread pool, not to be continued ...

Java Multithreading: Getting Started (1)-Simple test Executeorservice

Related Article

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.