当前位置: 首页 > news >正文

设计公司网站需要考虑什么临沂网站临沂网站制作

设计公司网站需要考虑什么,临沂网站临沂网站制作,公司官网的seo,gwt 网站开发AbstractExecutorService 上一篇文章中#xff0c;主要介绍了AbstractExecutorService的线程执行的核心流程#xff0c;execute() 这个方法显然是没有返回执行任务的结果#xff0c;如果我们需要获取任务执行的结果#xff0c;怎么办#xff1f; Callable 就是一个可以获…AbstractExecutorService 上一篇文章中主要介绍了AbstractExecutorService的线程执行的核心流程execute() 这个方法显然是没有返回执行任务的结果如果我们需要获取任务执行的结果怎么办 Callable 就是一个可以获取线程执行的结果。 public abstract class AbstractExecutorService implements ExecutorService {/** 将任务包装成FutureTask任务。带返回值参数的*/protected T RunnableFutureT newTaskFor(Runnable runnable, T value) {return new FutureTaskT(runnable, value);}/**** 不带返回值的**/protected T RunnableFutureT newTaskFor(CallableT callable) {return new FutureTaskT(callable);}/*** throws RejectedExecutionException {inheritDoc}* throws NullPointerException {inheritDoc}*/public Future? submit(Runnable task) {if (task null) throw new NullPointerException();//1.将任务包装成RunableFuture对象由于RunnableFuture是实现Runable类所以execute的参数是一个可拓展的类型RunnableFutureVoid ftask newTaskFor(task, null);//2交给具体的执行器进行实现execute(ftask);return ftask;}/*** throws RejectedExecutionException {inheritDoc}* throws NullPointerException {inheritDoc}*/public T FutureT submit(Runnable task, T result) {if (task null) throw new NullPointerException();RunnableFutureT ftask newTaskFor(task, result);execute(ftask);return ftask;}/*** throws RejectedExecutionException {inheritDoc}* throws NullPointerException {inheritDoc}*/public T FutureT submit(CallableT task) {if (task null) throw new NullPointerException();//将任务装成成一个FutureTask任务RunnableFutureT ftask newTaskFor(task);//执行任务execute(ftask);return ftask;}}submit其实是一个重载的方法分别是一个task以及可以传递获取结果的任务以及使用callable。 demo 从源码上看三个方法其实都是将任务进行了封装然后调用线程池执行的核心方法 public static void main(String[] args) throws ExecutionException, InterruptedException {CallableInteger resultCallable new CallableInteger() {Overridepublic Integer call() throws Exception {return 1 1;}};ExecutorService threadPool Executors.newFixedThreadPool(1);FutureInteger resultTask threadPool.submit(resultCallable);System.out.println(resultTask.get());threadPool.shutdown();}FutureTask public class FutureTaskV implements RunnableFutureV {/* NEW - COMPLETING - NORMAL* NEW - COMPLETING - EXCEPTIONAL* NEW - CANCELLED* NEW - INTERRUPTING - INTERRUPTED*/private volatile int state;private static final int NEW 0; // 初始化状态private static final int COMPLETING 1; // 结果计算完成或响应中断到赋值给返回值的状态private static final int NORMAL 2; // 任务正常完成结果被setprivate static final int EXCEPTIONAL 3; // 任务抛出异常private static final int CANCELLED 4; // 任务被取消private static final int INTERRUPTING 5; // 线程中断状态被设置为true 线程未响应中断private static final int INTERRUPTED 6; // 线程已被中断/** The underlying callable; nulled out after running */private CallableV callable; // 需要执行的任务/** The result to return or exception to throw from get() */// 执行callable的线程调用FutureTask.run()方法通过CAS设置private Object outcome; // non-volatile, protected by state reads/writes/** The thread running the callable; CASed during run() */// 执行callable的线程调用FutureTask.run()方法通过CAS设置private volatile Thread runner;/** Treiber stack of waiting threads */private volatile WaitNode waiters;public FutureTask(CallableV callable) {if (callable null)throw new NullPointerException();this.callable callable;this.state NEW; // 初始化状态是new // ensure visibility of callable} }/* 继承了Runnable 因为线程池中执行的也是Runnbale的任务*/ public interface RunnableFutureV extends Runnable, FutureV {/*** Sets this Future to the result of its computation* unless it has been cancelled.*/void run(); }FutureTask 实现RunnableFuture也间接实现了run方法。 重点 我们知道 execute(ftask); 本质就是利用线程池进行执行而线程执行的时候其实就是启动对应任务的run方法。 task.run();// 这里是什么时候调用的其实是// execute(ftask)传入的任务 task.run()public void run() {//不是新建状态 直接中止if (state ! NEW ||!UNSAFE.compareAndSwapObject(this, runnerOffset,null, Thread.currentThread()))return;try {CallableV c callable;if (c ! null state NEW) {V result;boolean ran;try {//核心执行任务的call方法你看就是调用普通的方法一样。result c.call();//同步调用获取结果值ran true;} catch (Throwable ex) {result null;ran false;setException(ex);}if (ran)//设置结果值set(result);}} finally {// runner must be non-null until state is settled to// prevent concurrent calls to run()runner null;// state must be re-read after nulling runner to prevent// leaked interruptsint s state;//响应中断if (s INTERRUPTING)handlePossibleCancellationInterrupt(s);}}判断当前任务状态非NEW直接返回执行对应c.call() 其实就是执行callable中的call方法。将返回值set进去 protected void set(V v) {//CAS 去设置当前任务执行状态 new-completingif (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {//返回结果outcomeoutcome v;UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final statefinishCompletion();}}get public V get() throws InterruptedException, ExecutionException {int s state;//如果是在执行中则等待一会if (s COMPLETING)s awaitDone(false, 0L);//返回结果return report(s);}/*** throws CancellationException {inheritDoc}*/public V get(long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException {if (unit null)throw new NullPointerException();//设置了超时时间则等待一定的时间如果还没有获取到返回异常int s state;if (s COMPLETING (s awaitDone(true, unit.toNanos(timeout))) COMPLETING)throw new TimeoutException();return report(s);}private V report(int s) throws ExecutionException {Object x outcome;//执行完成 返回x结果if (s NORMAL)return (V)x;//如果任务取消返回异常if (s CANCELLED)throw new CancellationException();throw new ExecutionException((Throwable)x);}awaitDone private int awaitDone(boolean timed, long nanos)throws InterruptedException {final long deadline timed ? System.nanoTime() nanos : 0L;WaitNode q null;boolean queued false;for (;;) {//如果线程执行interrupted直接抛出异常并且将任务移除if (Thread.interrupted()) {removeWaiter(q);throw new InterruptedException();}int s state;//状态大于COMPLETING 说明完成了if (s COMPLETING) {if (q ! null)q.thread null;return s;}//else if (s COMPLETING) // cannot time out yetThread.yield();else if (q null)q new WaitNode();else if (!queued)queued UNSAFE.compareAndSwapObject(this, waitersOffset,q.next waiters, q);else if (timed) {nanos deadline - System.nanoTime();if (nanos 0L) {removeWaiter(q);return state;}LockSupport.parkNanos(this, nanos);}elseLockSupport.park(this);}}小结 FutureTask是一个支持取消行为的异步任务执行器。该类实现了Future接口的方法。 如 取消任务执行查询任务是否执行完成获取任务执行结果”get“任务必须得执行完成才能获取结果否则会阻塞直至任务完成。 如果在当前线程中需要执行比较耗时的操作但又不想阻塞当前线程时可以把这些作业交给FutureTask另开一个线程在后台完成当当前线程将来需要时就可以通过FutureTask对象获得后台作业的计算结果或者执行状态。 Future模式其实是多线程编程中常用的设计模式主线程向另外一个线程提交任务无需等待任务执行的结果返回一个凭证就是future通过future.get()去获取结果。这个过程可能是阻塞的。
http://www.eeditor.cn/news/126089/

相关文章:

  • 网站建设人员培训用html做的网站加背景音乐
  • 会计网站模板湖州做网站优化
  • 二级域名做网址导航大全网站温州好的网站推广
  • 企业网站建立的失败案例网站欢迎页代码
  • cms大型门户网站 源码网页设计实训报告任务书
  • 甘肃手机版建站系统价格营销网站开发系统
  • 如何做网站词库江苏省建筑网监督信息平台
  • 打开网站弹出qq网站子站建设
  • 张店低价网站建设网站如何选择服务器
  • 为什么备案关闭网站网站开发报价表格
  • 网站开发通用流程图代做网站推广的公司
  • 中国建设局网站临海市住房与城乡建设规划局网站
  • 用.net做网站好 还是用php网站建设的相关新闻
  • 有教做点心的网站吗莆田网站自助建站
  • 网站建设分工明细表宁波网站制作首荐荣盛网络好
  • 做暧暧视频免费视频网站海沧区建设局网站市政处
  • 网站流量超昌吉网站建设电话
  • wordpress视频页面沙洋县seo优化排名价格
  • 给人做网站做一个15页的网站怎么做
  • 太原工程建设信息网站百度seo怎么收费
  • 用什么软件来做网站山东网站建设模板制作
  • 电子商务网站建设与管理期末考试网站建设高校
  • 做网站建设公司企业物联网设计论文
  • 电子商务网站模块免费ppt模板下载网址不需要会员
  • 石家庄网站推广专业濮阳市建设工程交易网
  • 网站列表页是啥浙江大成建设集团有限公司网站
  • 黄页网站介绍制作网站建网站
  • 网站域名必须备案吗怎么把网站放到服务器上
  • 响应式网站建设推荐乐云践新金方时代网站建设
  • 学习网站建设的心得体会贵港做网站化司