Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the result of Java thread pool after running?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report