私は、さまざまなシングルトンパターンを作成し、数百万のスレッドを使ってブレークをチェックしようとしています。私はこれが最終的にBill Pughを実装するようになると思っていました。しかし、私は古典的なものを壊すことさえできません。古典的な遅延初期化を壊すことはできません。シングルトン
シングルトン:以前は100万のスレッドを試しましたが、すべて同じハッシュコードを使用していました。だから私は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);
}
}
?
このコードを試してみてください、まだ私のための単一のハッシュコードを返します。 'Thread.sleep'はデフォルトで現在のスレッドもスリープしません。 – garg10may
私のシステムで試してみました。はい、どちらも同じことをします。私は両方のステートメントで自分のアンサーチェックを掲載しました。 – cody123
「スリープ」が必要なのはなぜだと思いますか? 'Thread'作成コードを短縮できますか?それらのRunnableは同じことをしています。 –