多线程2

1.

 1 public class MultiThread {
 2     private static int num = 0;
 3     
 4     public static synchronized void printNum(String tag){
 5         if("a".equals(tag)){
 6             System.out.println("tag a, set num over");
 7             num = 100;
 8         } else {
 9             System.out.println("tag b, set num over");
10             num = 200;
11         }
12         System.out.println("tag:"+tag+",num=" + num);
13     }
14 
15     public static void main(String[] args) {
16         final MultiThread multiThread1 = new MultiThread();
17         final MultiThread multiThread2 = new MultiThread();
18         
19         Thread t1 = new Thread(new Runnable() {
20             @Override
21             public void run() {
22                 multiThread1.printNum("a");
23             }
24         });
25         
26         Thread t2 = new Thread(new Runnable() {
27             @Override
28             public void run() {
29                 multiThread2.printNum("b");
30             }
31         });
32         
33         t1.start();
34         t2.start();
35         
36         /*
37          *  tag a, set num over
38             tag:a,num=100
39             tag b, set num over
40             tag:b,num=200
41          */
42     }
43 }
View Code

 

posted @ 2017-12-01 19:50  黑土白云  阅读(147)  评论(0编辑  收藏  举报