Javatpoint Logo
Javatpoint Logo

Java ThreadPoolExecutor execute() Method

The 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.

Syntax

Parameter

command - the task to execute

Returns

Does not return value.

Throws

RejectedExecutionException - at the discretion of RejectedExecutionHandler, if the task cannot be accepted for execution

NullPointerException - if the command is null

Example 1

Test it Now

Output:

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 2

Test it Now

Output:

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 :






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA