The interface for receiving the query of red envelope rights timed out before, because some users ordered a little too much rights

The solution

FutureTask was chosen because it only executes the run() method once (even if it is called multiple times), avoiding the possibility of repeated queries. The asynchronous execution of multiple tasks can also improve the interface response speed.

This article focuses on an example of thread pooling with asynchronous execution of FutureTask

Thread pool +FutureTask performs multi-task computation

Public class Test {// Thread pool is best used as a global variable, If used locally, shutdown() ThreadFactory namedThreadFactory = new ThreadFactoryBuilder (). SetNameFormat (" thread - start - runner - % d "). The build (); The ExecutorService taskExe = new ThreadPoolExecutor (10,20,800 L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(100),namedThreadFactory); int count=0; @test public void Test (String[] args) {// List<FutureTask<Integer>> taskList=new ArrayList<FutureTask<Integer>>(); for(int i=0; i<100; I++){FutureTask<Integer> FutureTask =new FutureTask<Integer>(new Callable<Integer>() {@override public Integer call() throws Exception { return 1; }}); Tasklist.add (FutureTask); taskList.add(FutureTask); taskExe.submit(futureTask); Try {for(FutureTask<Integer> FutureTask :taskList){count+= futuretask.get (); }} catch (InterruptedException e) {logger.error(" Thread execution is interrupted ",e); } catch (ExecutionException e) {logger.error(" thread ExecutionException ",e); } // Close the thread pool taskexe.shutdown (); // Print: 100 system.out.println (count); }}Copy the code

The Callable interface lets us get the result of the thread’s execution, so let it be an input to the FutureTask constructor, FutureTask(Callable

Callable).

The result of a FutureTask execution is put into its private outcome variable, which other threads simply call FutureTask.get () to read

Second, the child thread out of the exception thrown out of the situation

Submit (Runnable task)

The run() block inside FutureTask swallows the exception and assigns the exception to the outcome via setException(Throwable t), which is returned when we call FutureTask.get() to get the result

If your code doesn’t call futureTask.get (), it won’t spit out the exception, and the child thread might somehow stop.

public Future<? > submit(Runnable task) { if (task == null) throw new NullPointerException(); RunnableFuture<Void> fTask = newTaskFor(task, null); execute(ftask); return ftask; }Copy the code

After the child thread is created, it executes the run() block inside FutureTask, which has a try-catch inside to intercept the thrown exception and assign it to the object

The above example does not have this problem because futureTask.get () is called and an exception is pulled from it

Source | blog.csdn.net/qq_44384533…


Recommend 3 original Springboot +Vue projects, with complete video explanation and documentation and source code:

Build a complete project from Springboot+ ElasticSearch + Canal

  • Video tutorial: www.bilibili.com/video/BV1Jq…
  • A complete development documents: www.zhuawaba.com/post/124
  • Online demos: www.zhuawaba.com/dailyhub

【VueAdmin】 hand to hand teach you to develop SpringBoot+Jwt+Vue back-end separation management system

  • Full 800 – minute video tutorial: www.bilibili.com/video/BV1af…
  • Complete development document front end: www.zhuawaba.com/post/18
  • Full development documentation backend: www.zhuawaba.com/post/19

【VueBlog】 Based on SpringBoot+Vue development of the front and back end separation blog project complete teaching

  • Full 200 – minute video tutorial: www.bilibili.com/video/BV1af…
  • Full development documentation: www.zhuawaba.com/post/17

If you have any questions, please come to my official account [Java Q&A Society] and ask me