mirror of
https://github.com/Estom/notes.git
synced 2026-02-07 04:23:55 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
101
Java/JavaDemo/codedemo/java/util/future/Future.ucls
Normal file
101
Java/JavaDemo/codedemo/java/util/future/Future.ucls
Normal file
@@ -0,0 +1,101 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.6" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
||||
associations="true" dependencies="false" nesting-relationships="true">
|
||||
<interface id="1" language="java" name="java.util.concurrent.Future" project="JavaDemo"
|
||||
file="/devdata/java/jdk/jdk1.7.0_60/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="334" y="85"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true"
|
||||
visibility="true">
|
||||
<attributes public="false" package="true" protected="false" private="false" static="false"/>
|
||||
<operations public="true" package="true" protected="false" private="false" static="false"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="2" language="java" name="java.util.concurrent.FutureTask" project="JavaDemo"
|
||||
file="/devdata/java/jdk/jdk1.7.0_60/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="521" y="477"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true"
|
||||
visibility="true">
|
||||
<attributes public="false" package="true" protected="false" private="false" static="false"/>
|
||||
<operations public="true" package="true" protected="false" private="false" static="false"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="3" language="java" name="java.util.concurrent.RunnableFuture" project="JavaDemo"
|
||||
file="/devdata/java/jdk/jdk1.7.0_60/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="521" y="280"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true"
|
||||
visibility="true">
|
||||
<attributes public="false" package="true" protected="false" private="false" static="false"/>
|
||||
<operations public="true" package="true" protected="false" private="false" static="false"/>
|
||||
</display>
|
||||
</interface>
|
||||
<interface id="4" language="java" name="java.util.concurrent.RunnableScheduledFuture" project="JavaDemo"
|
||||
file="/devdata/java/jdk/jdk1.7.0_60/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="130" y="410"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true"
|
||||
visibility="true">
|
||||
<attributes public="false" package="true" protected="false" private="false" static="false"/>
|
||||
<operations public="true" package="true" protected="false" private="false" static="false"/>
|
||||
</display>
|
||||
</interface>
|
||||
<interface id="5" language="java" name="java.util.concurrent.ScheduledFuture" project="JavaDemo"
|
||||
file="/devdata/java/jdk/jdk1.7.0_60/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="131" y="272"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true"
|
||||
visibility="true">
|
||||
<attributes public="false" package="true" protected="false" private="false" static="false"/>
|
||||
<operations public="true" package="true" protected="false" private="false" static="false"/>
|
||||
</display>
|
||||
</interface>
|
||||
<interface id="6" language="java" name="java.util.concurrent.Delayed" project="JavaDemo"
|
||||
file="/devdata/java/jdk/jdk1.7.0_60/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="131" y="119"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true"
|
||||
visibility="true">
|
||||
<attributes public="false" package="true" protected="false" private="false" static="false"/>
|
||||
<operations public="true" package="true" protected="false" private="false" static="false"/>
|
||||
</display>
|
||||
</interface>
|
||||
<interface id="7" language="java" name="java.lang.Runnable" project="JavaDemo"
|
||||
file="/devdata/java/jdk/jdk1.7.0_60/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="521" y="119"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true"
|
||||
visibility="true">
|
||||
<attributes public="false" package="true" protected="false" private="false" static="false"/>
|
||||
<operations public="true" package="true" protected="false" private="false" static="false"/>
|
||||
</display>
|
||||
</interface>
|
||||
<generalization id="8">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</generalization>
|
||||
<generalization id="9">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="7"/>
|
||||
</generalization>
|
||||
<generalization id="10">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</generalization>
|
||||
<generalization id="11">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</generalization>
|
||||
<generalization id="12">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</generalization>
|
||||
<generalization id="13">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</generalization>
|
||||
<realization id="14">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</realization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
accessors="true" visibility="true">
|
||||
<attributes public="false" package="true" protected="false" private="false" static="false"/>
|
||||
<operations public="true" package="true" protected="false" private="false" static="false"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
||||
106
Java/JavaDemo/codedemo/java/util/future/HelloFuture.java
Normal file
106
Java/JavaDemo/codedemo/java/util/future/HelloFuture.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package cn.aofeng.demo.java.util.future;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* Future、Callable练习。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class HelloFuture {
|
||||
|
||||
private static Logger _logger = Logger.getLogger(HelloFuture.class);
|
||||
|
||||
/**
|
||||
* @param args
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
executeSingleTask();
|
||||
executeBatchTask();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行单个异步任务:<br>
|
||||
* 1、用submit方法向线程池提交一个任务。<br>
|
||||
* 2、获取结果时,指定了超时时间。<br><br>
|
||||
* 结果:超时后,调用者收到TimeoutException,但实际上任务还在继续执行。
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private static void executeSingleTask() throws InterruptedException {
|
||||
ExecutorService threadpool = Executors.newFixedThreadPool(6);
|
||||
try {
|
||||
Future<Integer> f = threadpool.submit(createCallable(5 * 1000));
|
||||
Object result = f.get(3, TimeUnit.SECONDS);
|
||||
System.out.println("单个任务的执行结果:"+result);
|
||||
} catch (Exception e) {
|
||||
_logger.error("线程执行任务时出错", e);
|
||||
} finally {
|
||||
threadpool.shutdown();
|
||||
threadpool.awaitTermination(10, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行多个异步任务:<br>
|
||||
* 1、用invokeAll方法向线程池提交多个任务,并指定了执行的超时时间。<br><br>
|
||||
* 结果:超时后,未执行完成的任务被取消,在调用Future的get方法时,取消的任务会抛出CancellationException,执行完成的任务可获得结果。
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private static void executeBatchTask() throws InterruptedException {
|
||||
ExecutorService threadpool = Executors.newFixedThreadPool(6);
|
||||
List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
|
||||
tasks.add(createCallable(2000));
|
||||
tasks.add(createCallable(5000));
|
||||
tasks.add(createCallable(2500));
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
try {
|
||||
List<Future<Integer>> fs = threadpool.invokeAll(tasks, 3, TimeUnit.SECONDS);
|
||||
int result = 0;
|
||||
for (Future<Integer> f : fs) {
|
||||
try{
|
||||
result += f.get();
|
||||
} catch(CancellationException ce) {
|
||||
// nothing
|
||||
}
|
||||
}
|
||||
System.out.println("执行三个任务共耗时:" + (System.currentTimeMillis() - startTime) + "毫秒");
|
||||
System.out.println("三个任务的执行结果汇总:"+result);
|
||||
} catch (Exception e) {
|
||||
_logger.error("线程执行任务时出错", e);
|
||||
} finally {
|
||||
threadpool.shutdown();
|
||||
threadpool.awaitTermination(10, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建需要长时间执行的任务模拟对象。
|
||||
* @param sleepTimes 线程休眠时间(单位:毫秒)
|
||||
* @return {@link Callable}对象
|
||||
*/
|
||||
private static Callable<Integer> createCallable(final int sleepTimes) {
|
||||
Callable<Integer> c = new Callable<Integer>() {
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
Thread.sleep(sleepTimes);
|
||||
System.out.println(Thread.currentThread().getName() + ": I'm working");
|
||||
return 9;
|
||||
}
|
||||
};
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user