FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
designPatterns/SingletonMode/Singleton.java at main · husuting/designPatterns · GitHub
husuting
/
designPatterns
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
designPatterns
/
SingletonMode
/
Singleton.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (32 loc) · 1.18 KB
Breadcrumbs
designPatterns
/
SingletonMode
/
Singleton.java
Copy path
File metadata and controls
37 lines (32 loc) · 1.18 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
designPattern
.
SingletonMode
;
/**
* 单例模式
*
* 问题:单例模式因为也是一个类,因此类加载器也可以同时产生它的实例,因此,要避免有同一个类的多个类加载器
* @data2021/9/13,19:06
* @authorsutinghu
*/
public
class
Singleton
{
/**
* volatile 保护多线程下不出现问题
* 为什么不直接在类加载时创建实例?这样直接避免了类的多线程问题
* 因为类被创建后,如果没有被引用,会被垃圾回收
* 所以从性能和空间以及垃圾回收的角度 都应该在需要使用的时候才被创建
*/
private
static
volatile
Singleton
uniqueIntance
;
/**
* 私有化构造器类 被写在Java手册中
* 其作用在使使用单例的类能够通过外部请求的方式创建 而不是外部直接创建
*/
private
Singleton
(){}
/**
* 多线程下获取单例模式,避免单一类被频繁调用时,出现混乱
* @return
*/
public
static
synchronized
Singleton
getInstance
(){
if
(
uniqueIntance
==
null
){
uniqueIntance
=
new
Singleton
();
}
return
uniqueIntance
;
}
}
Back
|
FazBrowse Home
|
New Git URL