Interface thread suspended animation project a long time without results

In some projects often call for no reason does not respond, this time how to deal with it?

Here proposes a solution.

It is done using threads control.

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class Test {
    
    private static ExecutorService exec = Executors.newFixedThreadPool(1);
    public static void main(String[] agst){
        try {
            //Callabal<String> string: 是返回值类型
            Callable<String> callable = new Callable<String>() {
                public String call() throws Exception{
                    Thread.sleep ( 2010 );
                     // method needs to call or interfaces
                     // if the interface has a return value directly return return value 
                    return "Success" ;
                }
            };
            Future <String> = Submit exec.submit (Callable);
             // Result return value of the interface, get (2 "Time", TimeUnit.SECONDS "Time Type") 
            String submit.get Result = (2 , TimeUnit.SECONDS);
             // own logic 
        } the catch (Exception E) {
            System.out.println(e);
        }
    }
}

Modify the code did not do the test, then use the test when in use.

 

Guess you like

Origin www.cnblogs.com/brent-leon/p/11975369.html