多线程1

Posted 黑土白云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程1相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

线程学习知识点总结

多个请求是多线程吗

多线程编程

多线程编程

python小白学习记录 多线程爬取ts片段

[Python3] 043 多线程 简介