import java.util.concurrent.*; public class FutureTaskTest { public static void main(String[] args) { // 线程1 Callable<String> tempCall1 = new Callable<String>(){ @Override public String call() throws Exception { Thread.sleep(5000); String back = "success"; System.out.println("线程1执行了"); return back; } }; // 线程2 Callable<String> tempCall2 = new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(5000); System.out.println("线程2执行了"); return "fail"; } }; ExecutorService executorService = Executors.newFixedThreadPool(10); Future<String> future1 = executorService.submit(tempCall1); System.out.println("线程的状态是否取消:"+future1.isCancelled()); String result1 = ""; try { result1 = future1.get(4000, TimeUnit.MILLISECONDS); }catch (TimeoutException e) { System.out.println("线程1超时"); future1.cancel(true); } catch (InterruptedException e) { System.out.println("线程1执行超时"); e.printStackTrace(); }catch(Exception e) { System.out.println("线程1执行异常"); e.printStackTrace(); } System.out.println("线程1返回的结果:"+result1); Future<String> future2 = executorService.submit(tempCall2); String result2 = ""; try { result2 = future2.get(6000,TimeUnit.MILLISECONDS); }catch (Exception e) { e.printStackTrace(); } System.out.println("线程2返回的结果:"+result2); executorService.shutdown(); } }
相关推荐
Hermes是一款基于Netty的可以支持百万级别的并发连接的高性能、高度可扩展的的网络通讯框架,它参考了dubbo和sofa-bolt的...callback future 支持客户端/服务端异步化编程 超时控制 使用SPI扩展点加载,扩展性强 鉴权
前言 本文主要给大家介绍了关于python中用Future对象回调别的...def callback(future, n): print('{}: future done: {}'.format(n, future.result())) async def register_callbacks(all_done): print('registering
未来元素选择器使用不带jQuery的jQuery的Future Elements Selector的替代品。 ###用法在您的项目中包括future.js: < script src =" ./future.js " > </ script > 传递参数以绑定功能: bind ( ...
python-callback-sample Python + Flask +服务生+后台任务安装并执行源service_install.sh取消服务源service_uninstall.shTODOS: 使用asyncio处理子流程: ://docs.python.org/pt-br/3.7/library/asyncio-...
% KEY1('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in KEY1.M with the given input arguments. % % KEY1('Property','Value',...) creates a new KEY1 or raises the %...
zebra-dao是在mybatis基础上进一步封装的异步DAO,同时它也支持分页功能... 支持Callback和Future两种异步化方式 支持分页功能 其他功能的使用方式和mybatis一致 标签:zebra
callback hell”)的混乱。 此API也是Java中Future / Promise并发构造的实现。 从Java 5开始,有一个用于异步编程的简单得多的API: Future接口及其基础实现FutureTask类。 Future接口表示异步计算的结果,并且只有...
第一步:创建你自己的Simulink模型,这个不用我讲吧,我们使用以下模型做演示: start_and_stop.mdl 第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_...
第一步:创建你自己的Simulink模型,这个不用我讲吧,我们使用以下模型做演示: start_and_stop.mdl 第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_...
第一步:创建你自己的Simulink模型,这个不用我讲吧,我们使用以下模型做演示: start_and_stop.mdl 第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_...
第一步:创建你自己的Simulink模型,这个不用我讲吧,我们使用以下模型做演示: start_and_stop.mdl 第二步:创建自己的GUI, 这个论坛里也有例子,我们使用以下文件。 start_...
future work Data Bind from DataSource Design Mode Support A icon for this control -- who can help me? Examples and Document Testing, if you find bugs, please tell me, thank you!
% CAISHUZI('CALLBACK') and CAISHUZI('CALLBACK',hObject,...) call the % local function named CALLBACK in CAISHUZI.M with the given input % arguments. % % *See GUI Options on GUIDE's Tools menu. Choose ...
'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn...
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to net1 (see VARARGIN) % Choose de
3、异步任务,使用future,callback 4、celery task,使用sqlite 或 redis 5、脚本部署启动。 文件如下: example.py run_shell.py setting.py stop.sh tc.db redis_manager.py server.py start.sh task.py ...
时间到使用超时检查器包装异步调用以防止它们无限期挂起例子 var timesUp = require ( 'times-up' ) ;function asyncFunc ( callback ) { setTimeout ( function ( ) { console . log ( 'Some process in the future...
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data Newtext=zheng2; handles.text=Newtext; guidata; set; 复制代码3.退出代码 function ...
function varargout = ganshe(varargin) % GANSHE M-file for ganshe.fig % GANSHE, by itself, ...% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user d
该项目的目标是使公开一个返回scala.concurrent.Future[T]的scala包装器变得非常容易,同时还使用 Play Framework 的WS库。 细节 假设我们定义了 Retrofit HTTP 接口: public interface GitHub { static class ...