-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo2.java
More file actions
43 lines (37 loc) · 957 Bytes
/
Copy pathDemo2.java
File metadata and controls
43 lines (37 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package sixPart;
/**
* @author yangxp
* @date 2017年8月9日 下午4:16:24
* <p>
* 这种方法在多线程条件下生成的对象是不一样的。
*/
public class Demo2 {
public static void main(String[] args) {
MyThread2Test myThread2Test1 = new MyThread2Test();
MyThread2Test myThread2Test2 = new MyThread2Test();
MyThread2Test myThread2Test3 = new MyThread2Test();
myThread2Test1.start();
myThread2Test2.start();
myThread2Test3.start();
}
}
class MyThread2Test extends Thread {
@Override
public void run() {
System.out.println(Demo2Test.getInstance().hashCode());
}
}
class Demo2Test {
private static Demo1Test demo1Test;
synchronized public static Demo1Test getInstance() {
if (demo1Test == null) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
demo1Test = new Demo1Test();
}
return demo1Test;
}
}