File tree Expand file tree Collapse file tree 9 files changed +269
-0
lines changed
src/main/java/com/chen/api/util/thread/study/chapter3 Expand file tree Collapse file tree 9 files changed +269
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .test1 ;
2+
3+ /**
4+ * 没有对象监视器导致抛出异常
5+ *
6+ * @author : chen weijie
7+ * @Date: 2018-04-24 00:01
8+ */
9+ public class Test1 {
10+
11+ public static void main (String [] args ) {
12+
13+ try {
14+ String newString = new String ("a" );
15+ newString .wait ();
16+ } catch (InterruptedException e ) {
17+ e .printStackTrace ();
18+ }
19+
20+
21+
22+ }
23+
24+
25+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .test2 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-04-24 00:11
6+ */
7+ public class Test {
8+
9+ public static void main (String [] args ) {
10+
11+ String lock =new String ("b" );
12+ System .out .println ("syn 上面" );
13+ synchronized (lock ){
14+ System .out .println ("syn第一行" );
15+ try {
16+ lock .wait ();
17+ System .out .println ("sync wait下的代码" );
18+ } catch (InterruptedException e ) {
19+ e .printStackTrace ();
20+ }
21+ }
22+ System .out .println ("syn下面的代码" );
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .test3 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-04-24 00:16
6+ */
7+ public class MyThread1 extends Thread {
8+
9+
10+ private Object lock ;
11+
12+ public MyThread1 (Object lock ) {
13+ super ();
14+ this .lock = lock ;
15+ }
16+
17+ @ Override
18+ public void run () {
19+
20+ try {
21+ synchronized (lock ) {
22+ System .out .println ("开始 wait time:" + System .currentTimeMillis ());
23+ lock .wait ();
24+ System .out .println ("结束 wait time:" + System .currentTimeMillis ());
25+ }
26+ } catch (InterruptedException e ) {
27+ e .printStackTrace ();
28+ }
29+
30+
31+ }
32+
33+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .test3 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-04-24 00:16
6+ */
7+ public class MyThread2 extends Thread {
8+
9+
10+ private Object lock ;
11+
12+ public MyThread2 (Object lock ) {
13+ super ();
14+ this .lock = lock ;
15+ }
16+
17+ @ Override
18+ public void run () {
19+ synchronized (lock ) {
20+ try {
21+ System .out .println ("开始 notify time:" + System .currentTimeMillis ());
22+ lock .notify ();
23+ System .out .println ("结束 notify time:" + System .currentTimeMillis ());
24+ } catch (Exception e ) {
25+ e .printStackTrace ();
26+ }
27+
28+
29+ }
30+ }
31+
32+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .test3 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-04-24 00:24
6+ */
7+ public class Test {
8+
9+ public static void main (String [] args ) {
10+
11+ Object lock = new Object ();
12+ MyThread1 thread1 = new MyThread1 (lock );
13+ thread1 .setName ("a" );
14+ thread1 .start ();
15+ try {
16+ Thread .sleep (3000 );
17+ } catch (InterruptedException e ) {
18+ e .printStackTrace ();
19+ }
20+ MyThread2 thread2 = new MyThread2 (lock );
21+ thread2 .setName ("b" );
22+ thread2 .start ();
23+
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .wait_notify_size5 ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ /**
7+ * @author : chen weijie
8+ * @Date: 2018-04-24 00:36
9+ */
10+ public class MyList {
11+
12+ private static List list = new ArrayList ();
13+
14+ public static void add () {
15+ list .add ("anyString" );
16+ }
17+
18+ public static int size () {
19+ return list .size ();
20+ }
21+
22+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .wait_notify_size5 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-04-24 00:49
6+ */
7+ public class Run {
8+
9+
10+ public static void main (String [] args ) {
11+
12+ Object lock = new Object ();
13+ ThreadA threadA = new ThreadA (lock );
14+ threadA .setName ("a" );
15+ threadA .start ();
16+
17+ try {
18+ Thread .sleep (500 );
19+ } catch (InterruptedException e ) {
20+ e .printStackTrace ();
21+ }
22+
23+ ThreadB threadB = new ThreadB (lock );
24+ threadB .setName ("b" );
25+ threadB .start ();
26+
27+
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .wait_notify_size5 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2018-04-24 00:39
6+ */
7+ public class ThreadA extends Thread {
8+
9+
10+ private Object object ;
11+
12+ public ThreadA (Object object ) {
13+ super ();
14+ this .object = object ;
15+ }
16+
17+ @ Override
18+ public void run () {
19+
20+ synchronized (object ) {
21+ try {
22+ if (MyList .size () != 5 ) {
23+ System .out .println ("wait begin..." + System .currentTimeMillis ());
24+ object .wait ();
25+ System .out .println ("wait end..." + System .currentTimeMillis ());
26+ }
27+ } catch (InterruptedException e ) {
28+ e .printStackTrace ();
29+ }
30+
31+
32+ }
33+
34+
35+ }
36+
37+
38+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter3 .wait_notify_size5 ;
2+
3+ /**
4+ * 由输出的日志可以看出,当执行notify方法后,不会立即释放锁,只有方法执行完毕才会释放锁。
5+ * @author : chen weijie
6+ * @Date: 2018-04-24 00:45
7+ */
8+ public class ThreadB extends Thread {
9+
10+
11+ private Object object ;
12+
13+ public ThreadB (Object ob ) {
14+ super ();
15+ this .object = ob ;
16+ }
17+
18+ @ Override
19+ public void run () {
20+ synchronized (object ) {
21+ try {
22+ for (int i = 0 ; i < 10 ; i ++) {
23+ MyList .add ();
24+ if (MyList .size () == 5 ) {
25+ System .out .println (" notify start===" + System .currentTimeMillis ());
26+ object .notify ();
27+ System .out .println ("notify end===" + System .currentTimeMillis ());
28+ }
29+ System .out .println ("添加了" + (i + 1 ) + "个元素" );
30+
31+ Thread .sleep (1000 );
32+ }
33+ } catch (InterruptedException e ) {
34+ e .printStackTrace ();
35+ }
36+
37+ }
38+ }
39+
40+ }
You can’t perform that action at this time.
0 commit comments