File tree Expand file tree Collapse file tree 1 file changed +3
-3
lines changed Expand file tree Collapse file tree 1 file changed +3
-3
lines changed Original file line number Diff line number Diff line change @@ -103,7 +103,7 @@ public class Singleton {
103
103
104
104
只需要对 getUniqueInstance() 方法加锁,就能让该方法一次只能一个线程访问,从而避免了对 uniqueInstance 变量进行多次实例化的问题。但是这样有一个问题是一次只能一个线程进入,性能上会有一定的浪费。
105
105
106
- ```
106
+ ``` java
107
107
public static synchronized Singleton getUniqueInstance() {
108
108
if (uniqueInstance == null ) {
109
109
uniqueInstance = new Singleton ();
@@ -123,7 +123,7 @@ private static Singleton uniqueInstance = new Singleton();
123
123
124
124
考虑第一个解决方案,它是直接对 getUniqueInstance() 方法进行加锁,而实际上只需要对 uniqueInstance = new Singleton(); 这条语句加锁即可。使用两个条件语句来判断 uniqueInstance 是否已经实例化,如果没有实例化才需要加锁。
125
125
126
- ```
126
+ ``` java
127
127
public class Singleton {
128
128
private volatile static Singleton uniqueInstance;
129
129
private Singleton () {
@@ -1252,7 +1252,7 @@ private boolean less(int v, int w) {
1252
1252
1253
1253
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
1254
1254
1255
- ```
1255
+ ``` java
1256
1256
private PriorityQueue<Integer > maxHeap = new PriorityQueue<> ((o1, o2) - > o2- o1); // 实现左边部分
1257
1257
private PriorityQueue<Integer > minHeep = new PriorityQueue<> (); // 实现右边部分,右边部分所有元素大于左边部分
1258
1258
private int cnt = 0 ;
You can’t perform that action at this time.
0 commit comments