[Java] Java program exit or perform other operations after it times out.

Source: Internet
Author: User

When the program enters an endless loop or cannot be terminated for other reasons, you need to force exit the program.

For the development software ECLIPSE, after the program execution times out, you can click the terminate button to force exit.

So, can we set a certain period of time through the program to terminate or perform other operations when the program runs beyond this period of time?


After checking a large amount of data, we found that the future class can meet this requirement.

Important methods in the future class include get () and cancel ().

Get () gets the data object. If the data is not loaded, it will block until the data is obtained, while cancel () cancels the data loading.

Another get (timeout) operation means that if no result is obtained within the timeout time, the system will fail to return, instead of blocking.

With these methods, we can achieve our requirements.

Java code example:

Final executorservice exec = executors. newfixedthreadpool (1); callable <string> call = new callable <string> () {Public String call () throws exception {// put the time-consuming operation code block int Cash = 300; string name = "zhangsan"; system. out. println (name + "now available" + cash + "yuan deposit"); User u = new user (name, cash); string [] arr = {"thread ", "thread B", "thread C", "thread D", "thread E", "thread F", "thread G", "thread H", "thread I ", "thread J"}; For (INT I = 0; I <10; I ++) {mythread th = new mythread (ARR [I], U, (INT) (math. random () * 1000-500); th. start ();} // time-consuming code block Ends Thread. sleep (1000*5); Return "thread execution completed" ;}}; try {future <string> future = exec. submit (CALL); string OBJ = Future. get (1000*1, timeunit. milliseconds); // The Task Processing timeout value is set to 1 second system. out. println ("returned successfully:" + OBJ);} catch (timeoutexception ex) {system. out. println ("processing timeout .... "); system. exit (0);} catch (exception e) {system. out. println ("processing failed. "); E. printstacktrace ();} exec. shutdown (); // close the thread pool

After you place the time-consuming code block in the annotation area, the requirements can be met.

System. Out. println ("processing failed."); E. printstacktrace ();

In this example, when the running times out, the exit operation is performed.

You can also add other code as needed for related operations.

For example, you can set to ignore the error and continue to execute the following command when the processing times out.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.