gpt4 book ai didi

java - 无法打破经典的延迟初始化单例

转载 作者:行者123 更新时间:2023-12-01 20:53:46 24 4
gpt4 key购买 nike

我正在尝试创建各种单例模式并使用数百万个线程检查中断。我希望这能引导我最终实现比尔·皮尤(Bill Pugh)。但我什至无法打破经典。

单例:之前尝试过一百万个线程,所有线程都具有相同的哈希码。所以我让它 hibernate 10 秒,这样两个线程都肯定会进入空检查条件,但一切都令人沮丧。

package demo2;

public class Singleton {

private static Singleton soleInstance = null;

private Singleton() throws InterruptedException {

}

public static Singleton getInstance() throws InterruptedException {

if (soleInstance == null) {

Thread.sleep(10000);

soleInstance = new Singleton();

}

return soleInstance;

}

}

测试类:

package demo2;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

class Test {

public int makeSingleton() throws InterruptedException {

Singleton s = Singleton.getInstance();

return s.hashCode();

}

public static void main(String[] args) throws InterruptedException, ExecutionException {

Test t = new Test();

ExecutorService executor = Executors.newFixedThreadPool(2);

List<Integer> list = new ArrayList<>();

for (int i = 0; i < 2; i++) {

Future<Integer> future = executor.submit(new Callable<Integer>() {

public Integer call() throws InterruptedException {

return t.makeSingleton();
}
});

list.add(future.get());
}

executor.shutdown();

List<Integer> list2 = list.stream().distinct().collect(Collectors.toList());

System.out.println(list2);

}
}

我该如何破解它?

最佳答案

下面提到的代码可以工作。

更改您的代码。可能您仅在方法内部调用 get,并且它正在等待获取结果并且循环计数不会增加。

ExecutorService executor = Executors.newFixedThreadPool(2);

List<Future<Integer>> list = new ArrayList<Future<Integer>>();

for (int i = 0; i < 5; i++) {

Future<Integer> future = executor.submit(new Callable<Integer>() {

public Integer call() throws InterruptedException {

return Singleton.getInstance().hashCode();
}
});

list.add(future);
}

executor.shutdown();

Set<Integer> output = new HashSet<Integer>();
for(Future<Integer> future : list){
output.add(future.get());
}

System.out.println(output);

关于java - 无法打破经典的延迟初始化单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42743444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com