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

多线程1

时间:2017-12-01 20:47:49      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:ide   extends   string   img   thread   pen   分享图片   new   stat   

1.

技术分享图片
 1 public class MyThread extends Thread{
 2     private  int count = 5;
 3     
 4     @Override
 5     public synchronized void run() {
 6         count--;
 7         System.out.println(Thread.currentThread().getName() + ":count="+count);
 8     }
 9 
10     public static void main(String[] args) {
11         /*MyThread t1 = new MyThread();
12         MyThread t2 = new MyThread();
13         MyThread t3= new MyThread();
14         MyThread t4 = new MyThread();
15         MyThread t5 = new MyThread();
16         t1.start();
17         t2.start();
18         t3.start();
19         t4.start();
20         t5.start();
21         结果:
22                 Thread-4:count=4
23                 Thread-1:count=4
24                 Thread-2:count=4
25                 Thread-3:count=4
26                 Thread-5:count=4 */
27         
28         MyThread myThread = new MyThread();
29         Thread t1 = new Thread(myThread, "t1");
30         Thread t2 = new Thread(myThread, "t2");
31         Thread t3 = new Thread(myThread, "t3");
32         Thread t4 = new Thread(myThread, "t4");
33         Thread t5 = new Thread(myThread, "t5");
34         t1.start();
35         t2.start();
36         t3.start();
37         t4.start();
38         t5.start();
39         /*
40             t1:count=4
41             t4:count=3
42             t2:count=2
43             t3:count=1
44             t5:count=0    
45          */
46     }
47 }
View Code

 

多线程1

标签:ide   extends   string   img   thread   pen   分享图片   new   stat   

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

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