futuretask

[高并发Java 七] 并发设计模式

 ̄綄美尐妖づ 提交于 2019-11-30 01:14:59
1. 什么是设计模式 在软件工程中,设计模式(design pattern)是对软件设计中普遍存在(反复出现)的各种问题 ,所提出的解决方案。这个术语是由埃里希·伽玛(Erich Gamma)等人在1990年代从建筑设计领 域引入到计算机科学的。 著名的4人帮: Erich Gamma, Richard Helm, Ralph Johnson ,John Vlissides (Gof) 《设计模式:可复用面向对象软件的基础》收录23种模式 2. 单例模式 单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为 比如:全局信息配置 单例模式最简单的实现: public class Singleton { private Singleton() { System.out.println("Singleton is create"); } private static Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } } 由私有构造方法和static来确定唯一性。 缺点:何时产生实例 不好控制 虽然我们知道,在类Singleton第一次被加载的时候,就产生了一个实例。

Is it a good way to use java.util.concurrent.FutureTask?

╄→гoц情女王★ 提交于 2019-11-29 19:37:41
First of all, I must say that I am quite new to the API java.util.concurrent, so maybe what I am doing is completely wrong. What do I want to do? I have a Java application that basically runs 2 separate processing (called myFirstProcess , mySecondProcess ), but these processing must be run at the same time. So, I tried to do that: public void startMyApplication() { ExecutorService executor = Executors.newFixedThreadPool(2); FutureTask<Object> futureOne = new FutureTask<Object>(myFirstProcess); FutureTask<Object> futureTwo = new FutureTask<Object>(mySecondProcess); executor.execute(futureOne);

How can I terminate Tasks that have timed out in multithreading?

醉酒当歌 提交于 2019-11-29 16:29:27
I need to make a library in which I will have synchronous and asynchronous methods in it. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. Core Logic of my Library The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by executing it and after we get the response back as a JSON String, we will send that JSON String back to

How to ensure garbage collection of a FutureTask that is submitted to a ThreadPoolExecutor and then cancelled?

时光怂恿深爱的人放手 提交于 2019-11-29 03:05:24
问题 I am submitting Callable objects to a ThreadPoolExecutor and they seem to be sticking around in memory. Looking at the heap dump with the MAT tool for Eclipse see that the Callable objects are being referenced by a FutureTask$Sync 's callable variable. That FutureTask$Sync is referenced by a FutureTask 's sync variable. That FutureTask is referenced by the FutureTask$Sync 's this$0 variable. I have read around about this (here, here, and on SO) and it seems like the FutureTask that the

Utility of Future.cancel(boolean) method

谁说胖子不能爱 提交于 2019-11-28 18:32:58
问题 I was simply exploring the java.util.concurrent package. I learnt that the class ' Future ' has a method boolean cancel(boolean mayInterruptIfRunning) Please find attached the test code I wrote : package com.java.util.concurrent; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; public class FutureTester { /** * @param args * @throws InterruptedException */ public

CompletableFuture, supplyAsync() and thenApply()

你离开我真会死。 提交于 2019-11-28 18:12:10
Need to confirm something. The following code: CompletableFuture .supplyAsync(() -> {return doSomethingAndReturnA();}) .thenApply(a -> convertToB(a)); would be the same as: CompletableFuture .supplyAsync(() -> { A a = doSomethingAndReturnA(); convertToB(a); }); Right? Furthermore, another two questions following as for "is there any reason why we would use thenApply ?" 1) having big code for conversion? or 2) need to reuse the lambda block in other places? It is not the same thing . In the second example where thenApply is not used it is certain that the call to convertToB is executed in the

What's the difference between Future and FutureTask in Java?

你。 提交于 2019-11-28 17:45:11
Since use ExecutorService can submit a Callable task and return a Future , why need to use FutureTask to wrap Callable task and use the method execute ? I feel they both do the same thing. In fact you are correct. The two approaches are identical. You generally don't need to wrap them yourself. If you are, you're likely duplicating the code in AbstractExecutorService: /** * Returns a <tt>RunnableFuture</tt> for the given callable task. * * @param callable the callable task being wrapped * @return a <tt>RunnableFuture</tt> which when run will call the * underlying callable and which, as a <tt

Is it a good way to use java.util.concurrent.FutureTask?

烈酒焚心 提交于 2019-11-28 15:33:00
问题 First of all, I must say that I am quite new to the API java.util.concurrent, so maybe what I am doing is completely wrong. What do I want to do? I have a Java application that basically runs 2 separate processing (called myFirstProcess , mySecondProcess ), but these processing must be run at the same time. So, I tried to do that: public void startMyApplication() { ExecutorService executor = Executors.newFixedThreadPool(2); FutureTask<Object> futureOne = new FutureTask<Object>(myFirstProcess)

FutureTask在缓存中使用

好久不见. 提交于 2019-11-28 15:22:54
缓存主要作用是提高应用程序吞吐量和响应性,当然也有负面影响,占用更多内存。在设计 SqlTemplate 也有个简单的本地缓存,sql模板实际只需要解释一次就可以了,以后的调用复用之前解释过。开始的时候是使用简单的HashMap实现的,但是在并发情况下会出现重复解释,下面是第一版的代码片段。 public class Configuration { private ConcurrentHashMap<String, SqlTemplate > templateCache; ...... public SqlTemplate getTemplate(final String content) { if (cacheTemplate) { //是 否则缓存模板 SqlTemplate sqlTemplate = templateCache.get(content); if (sqlTemplate != null){ sqlTemplate = createTemplate(content) ; templateCache.put(content, sqlTemplate) ; } return sqlTemplate; } return createTemplate(content); } //解释构建模板对象 private SqlTemplate createTemplate

How can I terminate Tasks that have timed out in multithreading?

柔情痞子 提交于 2019-11-28 11:18:27
问题 I need to make a library in which I will have synchronous and asynchronous methods in it. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. Core Logic of my Library The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by