广州网站建设流程图,辽宁建设厅证件查询网站,可以发外链的论坛有哪些,哪些网站可以做团购thread和runnable已经out了。取而代之的是callableV,它的结果存在futureV中。后者有get对象可以阻塞并最终获得异步结果。FutureTask既是callable又是future。可以作为一个执行单元。 直接启动一个thread执行callable是不明智的#xff0c;因为太多的短命的线…thread和runnable已经out了。取而代之的是callableV,它的结果存在futureV中。后者有get对象可以阻塞并最终获得异步结果。FutureTask既是callable又是future。可以作为一个执行单元。 直接启动一个thread执行callable是不明智的因为太多的短命的线程会影响jvm的性能。较好的办法是Executors的线程池。它有不同的方法创建不同的线程池 newCachedThreadPoolNew threads are created as needed; idle threads are kept for 60 seconds. newFixedThreadPoolThe pool contains a fixed set of threads; idle threads are kept indefinitely. newSingleThreadExecutorA “pool” with a single thread that executes the submitted tasks sequentially (similar to the Swing event dispatch thread). newScheduledThreadPoolA fixed-thread pool for scheduled execution; a replacement for java.util.Timer. newSingleThreadScheduledExecutorA single-thread “pool” for scheduled execution 当你有了一个callable你可以交给ExecutorService去执行通过submit。当然runnable和thread也可以不过他们没有返回值。 如果你有一组task需要执行你不必一个一个的去启动。ExecutorService的invokeAny和invokeAll可以帮你解决问题。你只需要处理返回的listfuture就可以了。ExecutorCompletionService是对ExecutorService的又一层封装可以帮助你依次获得结果。 如果你的目的不只是利用多线程执行一组任务而是需要多个线程互相协作比如生产者-消费者这样的问题。那么需要进行线程间的通讯。传统的wait和notify已经out了await和signal用起来也比较麻烦。如果你的需求满足一定的模式那么java自带哪些同步设施比如CyclicBarrierCountDownLatchExchangerSemaphoreSychronousQueue可以满足要求。具体的适用可以参见文档。 如果线程需要访问共享变量那么java的同步集合是需要考虑得其他的threadlocal也有应用场景。转载于:https://www.cnblogs.com/alphablox/archive/2013/02/17/2914373.html