java中断程序运行,超时中断
public class TimeCount {
final static int MAX_THREADS = 50 ;
final static long timeout = 7200 ;
public static boolean timec ( final DisposeService oldService, final Map map, final String tFlag) {
ExecutorService executorService = Executors. newFixedThreadPool ( MAX_THREADS) ;
boolean check = true ;
Callable< String> task = new Callable < String> ( ) {
@Override
public String call ( ) throws Exception {
oldService. submitData ( map, tFlag) ;
return "success" ;
}
} ;
Future< String> future = executorService. submit ( task) ;
try {
String rst = future. get ( timeout, TimeUnit. SECONDS) ;
System. out. println ( rst) ;
check = true ;
} catch ( TimeoutException e) {
System. out. println ( "执行超时" ) ;
future. cancel ( true ) ;
check = false ;
} catch ( Exception e) {
System. out. println ( "获取数据异常," + e. getMessage ( ) ) ;
check = false ;
} finally {
executorService. shutdown ( ) ;
while ( true ) {
if ( executorService. isTerminated ( ) ) {
System. out. println ( "关闭线程池" ) ;
break ;
}
}
}
return check;
}
}