码迷,mamicode.com
首页 > 编程语言 > 详细

多线程2

时间:2017-12-01 20:45:25      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:线程   isp   int   star   start   equals   play   col   str   

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

 

多线程2

标签:线程   isp   int   star   start   equals   play   col   str   

原文地址:http://www.cnblogs.com/bravolove/p/7944203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!