java异步任务处理

帮助理解异步的小程序

原文地址:https://www.cnblogs.com/chenmo-xpw/p/5652029.html


1、场景

 

  最近做项目的时候遇到了一个小问题:从前台提交到服务端A,A调用服务端B处理超时,原因是前端一次请求往db插1万数据,插完之后会去清理缓存、发送消息。

服务端的有三个操作 a、插DB b、清理cache  c、发送消息。1万条数据,说多不多,说少不少.况且不是单单insert。出现超时现象,不足为奇了吧~~

 

2、分析

 

  如何避免超时呢?一次请求处理辣么多数据,可分多次请求处理,每次请求处理100条数据,可以有效解决超时问题. 为了不影响用户的体验,请求改为ajax 异步请求。

除此之外,仔细分析下场景. a 操作是本次处理的核心. 而b、c操作可以异步处理。换句话说,a操作处理完可以马上返回给结果, 不必等b、c处理完再返回。b、c操作可以放在一个异步任务去处理。

 

3、实战

 

(1)、ExecutorService : 任务提交

 

(2)、demo

异步任务类

复制代码
public class ExecutorDemo {

    private ExecutorService executor = Executors.newFixedThreadPool(1);
    
    public void asynTask() throws InterruptedException {
        
        
        executor.submit(new Runnable() {
            
            @Override
            public void run() {
                
                try {
                    Thread.sleep(10000);//方便观察结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                
                int sum = 0;
                for(int i = 0; i < 1000; i++) {
                    
                    sum += i;
                }
                
                System.out.println(sum);
            }
        });
        
    }
}
复制代码

 客户端模拟

复制代码
public class Client {

    public static void main(String[] args) throws InterruptedException {
        
        boolean r = task2();
        
        if(r) {
            task3();
        }
        
        System.out.println("------------main end-----------");
    }
    
    static boolean task2() throws InterruptedException {
        
        ExecutorDemo e = new ExecutorDemo();
        
        e.asynTask();
        
        System.out.println("------------task2 end-----------");
        
        return true;
    }
    
    
    static void task3() throws InterruptedException {
        int j = 0;
        while(true) {
            if(j++ > 10000) {
                break;
            }
        }
        
        System.out.println("------------task3 end-----------");
    }
}
复制代码

 

结果是酱紫的

------------task2 end-----------
------------task3 end-----------
------------main end-----------
499500

猜你喜欢

转载自blog.csdn.net/w33365/article/details/79961153