Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the result of Java thread pool after running?

Shulou Source: shulou.com Published: 2022-06-02 12:40:27 09月14日 Update

What is the result of Java thread pool after running? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Java thread pool requires constant learning, and we have to pay attention to a lot of problems when we learn. Let's take a look at how the specific language operating environment can satisfy the running of Java thread pool-related programs.

Either the submit () method that receives a Runnable parameter or the Callable parameter returns an object of type Future (which is also an interface). This object contains the execution and results of the task. Call the boolean isDone () method of Future to know whether the task has been executed; call the Object get () method to get the return result after the task has been executed. If the task is not finished at this time, the get () method will wait until the corresponding task is finished.

Let's use the following example to demonstrate the use of the Java thread pool in Java5.0:

Java code

Import java.util.concurrent.*

Public class ExecutorTest {

Public static void main (String [] args) throws

InterruptedException

ExecutionException {

ExecutorService es = Executors.newSingleThreadExecutor ()

Future fr = es.submit (new RunnableTest ()); / / submit the task

Future fc = es.submit (new CallableTest ()); / / submit the task

/ / get the return value and output

System.out.println ((String) fc.get ())

/ / check whether the task is completed.

If (fr.isDone ()) {

System.out.println ("execution completed-RunnableTest.run ()")

} else {

System.out.println ("not finished-RunnableTest.run ()")

}

/ / check whether the task is completed.

If (fc.isDone ()) {

System.out.println ("execution completed-CallableTest.run ()")

} else {

System.out.println ("not finished-CallableTest.run ()")

}

/ / stop thread pool service

Es.shutdown ()

}

}

Class RunnableTest implements Runnable {

Public void run () {

System.out.println ("executed-RunnableTest.run ()")

}

}

Class CallableTest implements Callable {

Public Object call () {

System.out.println ("executed-CallableTest.call ()")

Return "return value-CallableTest.call ()"

}

}

Import java.util.concurrent.*

Public class ExecutorTest {

Public static void main (String [] args) throws

InterruptedException

ExecutionException {

ExecutorService es = Executors.newSingleThreadExecutor ()

Future fr = es.submit (new RunnableTest ()); / / submit the task

Future fc = es.submit (new CallableTest ()); / / submit the task

/ / get the return value and output

System.out.println ((String) fc.get ())

/ / check whether the task is completed.

If (fr.isDone ()) {

System.out.println ("execution completed-RunnableTest.run ()")

} else {

System.out.println ("not finished-RunnableTest.run ()")

}

/ / check whether the task is completed.

If (fc.isDone ()) {

System.out.println ("execution completed-CallableTest.run ()")

} else {

System.out.println ("not finished-CallableTest.run ()")

}

/ / stop thread pool service

Es.shutdown ()

}

}

Class RunnableTest implements Runnable {

Public void run () {

System.out.println ("executed-RunnableTest.run ()")

}

}

Class CallableTest implements Callable {

Public Object call () {

System.out.println ("executed-CallableTest.call ()")

Return "return value-CallableTest.call ()"

}

}

Running result:

Executed-RunnableTest.run ()

Executed-CallableTest.call ()

Return value-CallableTest.call ()

Execution completed-RunnableTest.run ()

Execution completed-CallableTest.run ()

After using the Java thread pool, you need to call its shutdown () method to stop the service, otherwise all threads in it will keep running and the program will not exit.

The answer to the question about the result of the Java thread pool after running is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Tasks threads results methods runs problems checks services parameters objects more programs learning help solutions output easy easy continuous code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker Shulou Tech Info Shulou Technology NVidia