Java ThreadPoolExecutor execute() MethodThe execute() method of ThreadPoolExecutor class executes the given task sometime in the future. The task may execute in a new thread or an existing pooled thread. If the task is not submitted to the pool due to any reason, then the task is handled by the current RejectedExecutionHandler. SyntaxParametercommand - the task to execute ReturnsDoes not return value. ThrowsRejectedExecutionException - at the discretion of RejectedExecutionHandler, if the task cannot be accepted for execution NullPointerException - if the command is null Example 1Test it NowOutput: Starting threads t2 going to sleep for 1000 milliseconds. t1 going to sleep for 1000 milliseconds. t3 going to sleep for 1000 milliseconds. Threads started, main ends t2 done sleeping t3 done sleeping t1 done sleeping Example 2Test it NowOutput: Starting threads Threads started, main ends Thread sleeping for 1000 i=0 Thread sleeping for 1000 i=1 Thread sleeping for 1000 i=2 Thread sleeping for 1000 i=3 Thread sleeping for 1000 i=4 now i > 5 :
Next TopicJava ThreadPoolExecutor
|