File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package Synchronized ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:
6+ * @date: Created in 2018/12/21
7+ */
8+ public class HasSelfPrivateNum {
9+ private int num = 0 ;
10+
11+ synchronized public void addI (String username ) {
12+ try {
13+ if (username .equals ("a" )) {
14+ num = 100 ;
15+ System .out .println ("a set over!" );
16+ //如果去掉hread.sleep(2000),那么运行结果就会显示为同步的效果
17+ Thread .sleep (2000 );
18+ } else {
19+ num = 200 ;
20+ System .out .println ("b set over!" );
21+ }
22+ System .out .println (username + " num=" + num );
23+ } catch (InterruptedException e ) {
24+ e .printStackTrace ();
25+ }
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package Synchronized ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:
6+ * @date: Created in 2018/12/21
7+ */
8+ public class Run {
9+ public static void main (String [] args ) {
10+ HasSelfPrivateNum numRef1 = new HasSelfPrivateNum ();
11+ HasSelfPrivateNum numRef2 = new HasSelfPrivateNum ();
12+ ThreadA threadA = new ThreadA (numRef1 );
13+ threadA .start ();
14+ ThreadB threadB = new ThreadB (numRef2 );
15+ threadB .start ();
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package Synchronized ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:
6+ * @date: Created in 2018/12/21
7+ */
8+ public class ThreadA extends Thread {
9+ private HasSelfPrivateNum numRef ;
10+
11+ public ThreadA (HasSelfPrivateNum numRef ) {
12+ super ();
13+ this .numRef = numRef ;
14+ }
15+
16+ @ Override
17+ public void run () {
18+ super .run ();
19+ numRef .addI ("a" );
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package Synchronized ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:
6+ * @date: Created in 2018/12/21
7+ */
8+ public class ThreadB extends Thread {
9+ private HasSelfPrivateNum numRef ;
10+
11+ public ThreadB (HasSelfPrivateNum numRef ) {
12+ super ();
13+ this .numRef = numRef ;
14+ }
15+
16+ @ Override
17+ public void run () {
18+ super .run ();
19+ numRef .addI ("b" );
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:使用interrupt()方法终止线程
6+ * @date: Created in 2018/12/21
7+ */
8+ public class InterruptDemo extends Thread {
9+ @ Override
10+ public void run () {
11+ super .run ();
12+ for (int i = 0 ; i < 500000 ; i ++) {
13+ if (this .interrupted ()) {
14+ System .out .println ("已经是停止状态了!我要退出了!" );
15+ // break; //使用break无法停止线程
16+ return ; //使用return停止线程
17+ }
18+ System .out .println ("i=" + (i + 1 ));
19+ }
20+ System .out .println ("看到这句话说明线程并未终止------" );
21+ }
22+
23+ public static void main (String [] args ) {
24+ try {
25+ InterruptDemo thread = new InterruptDemo ();
26+ thread .start ();
27+ Thread .sleep (2000 );
28+ thread .interrupt ();
29+ } catch (InterruptedException ex ) {
30+ System .out .println ("main catch" );
31+ ex .printStackTrace ();
32+ }
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:多个线程之间不共享变量线程安全的情况
6+ * @date: Created in 2018/12/21
7+ */
8+ public class MyThread1 extends Thread {
9+ private int count = 5 ;
10+
11+ public MyThread1 (String name ) {
12+ super ();
13+ this .setName (name );
14+ }
15+
16+ @ Override
17+ public void run () {
18+ super .run ();
19+ while (count > 0 ) {
20+ count --;
21+ System .out .println ("由 " + MyThread1 .currentThread ().getName () + " 计算,count=" + count );
22+ }
23+ }
24+
25+ public static void main (String [] args ) {
26+ MyThread1 thread1 = new MyThread1 ("A" );
27+ MyThread1 thread2 = new MyThread1 ("B" );
28+ MyThread1 thread3 = new MyThread1 ("C" );
29+ thread1 .start ();
30+ thread2 .start ();
31+ thread3 .start ();
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:多个线程之间共享变量线程不安全的情况
6+ * @date: Created in 2018/12/21
7+ */
8+ public class SharedVariableThread extends Thread {
9+ private int count = 5 ;
10+
11+ @ Override
12+ public void run () {
13+ super .run ();
14+ while (count > 0 ) {
15+ count --;
16+ System .out .println ("由 " + SharedVariableThread .currentThread ().getName () + " 计算,count=" + count );
17+ }
18+ }
19+
20+ public static void main (String [] args ) {
21+ SharedVariableThread thread = new SharedVariableThread ();
22+ Thread a = new Thread (thread , "A" );
23+ Thread b = new Thread (thread , "B" );
24+ Thread c = new Thread (thread , "C" );
25+ Thread d = new Thread (thread , "D" );
26+ Thread e = new Thread (thread , "E" );
27+ a .start ();
28+ b .start ();
29+ c .start ();
30+ d .start ();
31+ e .start ();
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments