Javatpoint Logo
Javatpoint Logo

submit() Method

The submit() method submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.

Syntax

The submit() method is overloaded in the following ways:

Parameter

task - the task to submit

Returns

Future representing the pending completion of the task

Throws

NullPointerException - if the task is null

RejectedExecutionException - if the task cannot be scheduled for execution

Example 1

Test it Now

Output:

numbers of core available in your processor:4
 Before invoking number of active thread   :0
 submit  :Taskk1@55f96302

Example 2

Test it Now

Output:

submit left :JavaForkJoinsubmitExample2@16019c77
submit left :JavaForkJoinsubmitExample2@18c670c7
submit left :JavaForkJoinsubmitExample2@55f96302
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 0 to 2
submit left :JavaForkJoinsubmitExample2@3d4eac69
submit left :JavaForkJoinsubmitExample2@42a57993
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 2 to 5
submit left :JavaForkJoinsubmitExample2@36dd1cc4
submit left :JavaForkJoinsubmitExample2@381ae46f
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 2 to 5
Thread[ForkJoinPool.commonPool-worker-1,5,main] computing: 2 to 5
Thread[ForkJoinPool.commonPool-worker-3,5,main] computing: 0 to 2
Thread[ForkJoinPool.commonPool-worker-3,5,main] computing: 0 to 2
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 0 to 2
submit left :JavaForkJoinsubmitExample2@75b84c92
Thread[main,5,main] computing: 7 to 10
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 5 to 7
submit left :JavaForkJoinsubmitExample2@64dcdaac
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 7 to 10
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 2 to 5
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 5 to 7
Thread[ForkJoinPool.commonPool-worker-2,5,main] computing: 7 to 10
Thread[ForkJoinPool.commonPool-worker-1,5,main] computing: 5 to 7
8
submit left :JavaForkJoinsubmitExample2@6bc7c054
submit left :JavaForkJoinsubmitExample2@6a1b3847
Thread[ForkJoinPool.commonPool-worker-3,5,main] computing: 7 to 10
Next TopicJava ForkJoinPool




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials