多线程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 }
以上是关于多线程1的主要内容,如果未能解决你的问题,请参考以下文章