1.单例
单例模式基本算是最简单,但使用最广泛的模式了, 本文不对常用写法(懒汉、恶汉、枚举等)多加赘述, 只给出并发模式中实现简单性能最优的方案:
public class Singleton {
private Singleton() {
System.out.println("Singleton is create");
}
private static class SingletonHolder {
private static Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
2. 不变模式
JDK中最长见到使用不变模式的例子就是字符串对象了(String), 对象一经创建就不会在发生变更, 对其进行的任何操作, 均是生成新的对象而不是修改元对象数据。 BigDecimal, Long, Integer等包装类, 均使用了不变模式。下面我们就实现一个简单的不变模式:
public final class ConstantPattern {
private final String id;
private final String name;
private final String description;
public ConstantPattern(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
3.Future模式
Future模式的核心思想是异步调用。通常调用一个方法时,我们会同步等待返回结果; 而有一些情况, 方法执行时间很长, 同时我们又不着急拿到结果, 因此可以不让被调用方法立即返回, 让它慢慢处理请求。 而调用者可以先处理其他任务, 等到需要用到前面的返回结果时, 再尝试获取数据, 这个过程其实就是Future模式的调用过程,如下图:
3.1 实现简单的Future模式
下面自己手写一段代码来实现简单的Future模式:
interface Data {
String getResult();
}
static class RealData implements Data {
protected final String result;
public RealData(String param) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 10; i++) {
sb.append(param);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.result = sb.toString();
}
@Override
public String getResult() {
return result;
}
}
static class FutureData implements Data {
protected RealData realData = null;
protected boolean isReady = false;
public synchronized void setRealData(RealData realData) {
if (isReady) {
return;
}
this.realData = realData;
isReady = true;
notifyAll();
}
@Override
public synchronized String getResult() {
while (!isReady) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return realData.getResult();
}
}
static class Client {
public Data request(final String queryStr) {
final FutureData feature = new FutureData();
new Thread(() -> {
RealData data = new RealData(queryStr);
future.setRealData(data);
}).start();
return feature;
}
}
public static void main(String[] args) {
Client client = new Client();
Data data = client.request("test");
System.out.println("请求完毕");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据=" + data.getResult());
}
3.2 JDK中的Future模式
示例代码如下:
static class RealData implements Callable<String> {
private String param;
public RealData(String param) {
this.param = param;
}
@Override
public String call() throws Exception {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 10; i++) {
sb.append(param);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> future = new FutureTask<String>(new RealData("test"));
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(future);
System.out.println("请求完毕");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据=" + future.get());
}
参考:
《Java高并发程序设计》