java异步02——多线程
用多线程的方式来实现异步效果。就是在主线程开启一个新线程去处理任务,然后去取这个任务结果。
一个在非异步的例子:
public class AsynchronousByUseThread {
public static void doSomethingA(){
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("--- doSomethingA---");
}
public static void doSomethingB(){
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("--- doSomethingB---");
}
public static void main(String[] args){
long start = System.currentTimeMillis();
doSomethingA();
doSomethingB();
System.out.println(System.currentTimeMillis()-start);
}
}
显式创建线程
手段创建一个线程,把方法在线程里执行
public class AsynchronousByUseThread {
public static void doSomethingA(){
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("--- doSomethingA---");
}
public static void doSomethingB(){
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("--- doSomethingB---");
}
public static void main(String[] args){
long start = System.currentTimeMillis();
Thread thread = new Thread(()->{ //把方法放在线程里执行
doSomethingA();
});
thread.start();
doSomethingB();
try {
thread.join(); //等待线程里的任务执行完
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(System.currentTimeMillis()-start);
}
}
线程池
没有返回值的任务
public class AsynchronousByUseThread {
private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS,
AVALIABLE_PROCESSORS*2,
1,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(5),
new ThreadPoolExecutor.CallerRunsPolicy());
public static void doSomethingA(){
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("--- doSomethingA---");
}
public static void doSomethingB(){
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("--- doSomethingB---");
}
public static void main(String[] args){
long start = System.currentTimeMillis();
POOL_EXECUTOR.execute(()->{
doSomethingA();
});
doSomethingB();
System.out.println(System.currentTimeMillis()-start);
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
有返回值的任务
public class AsynchronousByUseThread {
private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS,
AVALIABLE_PROCESSORS*2,
1,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(5),
new ThreadPoolExecutor.CallerRunsPolicy());
public static String doSomethingA(){
try{
Thread.sleep(2000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("--- doSomethingA---");
return "A Task Done";
}
public static void main(String[] args){
Future<String> result = POOL_EXECUTOR.submit(()->doSomethingA());
try {
System.out.println(result.get());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}