File tree Expand file tree Collapse file tree 26 files changed +536
-0
lines changed
src/main/java/com/chen/api/util/thread/study Expand file tree Collapse file tree 26 files changed +536
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton1 ;
2+
3+ /**
4+ * 饿汉模式
5+ *
6+ * @author : chen weijie
7+ * @Date: 2018-05-21 23:31
8+ */
9+ public class MyObject {
10+
11+ private static MyObject myObject = new MyObject ();
12+
13+ private MyObject () {
14+ }
15+
16+ public static MyObject getInstance () {
17+ //此代码版本为立即加载,此代码的缺点是不能有其他实例变量
18+ //因为getInstance()没有同步,所以有可能出现线程安全的问题
19+ return myObject ;
20+ }
21+
22+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton1 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:36
6+ */
7+ public class MyThread extends Thread {
8+
9+ @ Override
10+ public void run () {
11+ System .out .println (MyObject .getInstance ().hashCode ());
12+ }
13+
14+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton1 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:37
6+ */
7+ public class Run {
8+
9+
10+ public static void main (String [] args ) {
11+
12+
13+ MyThread myThreadA = new MyThread ();
14+ MyThread myThreadB = new MyThread ();
15+ MyThread myThreadC = new MyThread ();
16+ myThreadA .start ();
17+ myThreadB .start ();
18+ myThreadC .start ();
19+
20+
21+ }
22+
23+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton2 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:38
6+ */
7+ public class MyObject {
8+
9+ private static MyObject myObject = null ;
10+
11+ private MyObject () {
12+ }
13+
14+ //懒汉模式,在多线程的情况下会出现同步问题。
15+ public static MyObject getInstance () {
16+ try {
17+ if (myObject == null ) {
18+ Thread .sleep (1000 );
19+ myObject = new MyObject ();
20+ }
21+ } catch (InterruptedException e ) {
22+ e .printStackTrace ();
23+ }
24+ return myObject ;
25+ }
26+
27+
28+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton2 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:36
6+ */
7+ public class MyThread extends Thread {
8+
9+ @ Override
10+ public void run () {
11+ System .out .println (MyObject .getInstance ().hashCode ());
12+ }
13+
14+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton2 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:41
6+ */
7+ public class Run {
8+
9+ public static void main (String [] args ) {
10+
11+ MyThread myThread1 = new MyThread ();
12+ MyThread myThread2 = new MyThread ();
13+ MyThread myThread3 = new MyThread ();
14+
15+ myThread1 .start ();
16+ myThread2 .start ();
17+ myThread3 .start ();
18+
19+ }
20+
21+
22+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton3 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:48
6+ */
7+ public class MyObject {
8+
9+
10+ private static MyObject myObject = null ;
11+
12+ private MyObject () {
13+ }
14+
15+ //整个方法上锁,同步方法的效率太低了
16+ synchronized public static MyObject getInstance () {
17+ try {
18+ if (myObject == null ) {
19+ Thread .sleep (2000 );
20+ myObject = new MyObject ();
21+ }
22+ } catch (InterruptedException e ) {
23+ e .printStackTrace ();
24+ }
25+ return myObject ;
26+ }
27+
28+
29+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton3 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:36
6+ */
7+ public class MyThread extends Thread {
8+
9+ @ Override
10+ public void run () {
11+ System .out .println (MyObject .getInstance ().hashCode ());
12+ }
13+
14+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton3 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:41
6+ */
7+ public class Run {
8+
9+ public static void main (String [] args ) {
10+
11+ MyThread myThread11 = new MyThread ();
12+ MyThread myThread22 = new MyThread ();
13+ MyThread myThread33 = new MyThread ();
14+
15+ myThread11 .start ();
16+ myThread22 .start ();
17+ myThread33 .start ();
18+
19+ }
20+
21+
22+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter6 .singleton4 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-05-21 23:54
6+ */
7+ public class MyObject {
8+
9+ private static MyObject myObject = null ;
10+
11+ private MyObject () {
12+ }
13+
14+ public static MyObject getInstance () {
15+ //同步代码块 getInstance方法中的所有代码都上锁,这样做也会降低运行效率
16+ synchronized (MyObject .class ) {
17+ if (myObject == null ) {
18+ myObject = new MyObject ();
19+ }
20+ }
21+ return myObject ;
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments