002JAVA多个多线程同时竞争一个类实例方法
package com.skcc.mthread;
public class MyThread001 extends Thread {
private int count=4;
public MyThread001() {
// TODO Auto-generated constructor stub
}
@Override
public synchronized void run() {
count--;
System.out.println(Thread.currentThread().getName() + " = " + count);
}
/***
* 多个线程同时竞争MyThread001类实例对象及成员方法
* 多个线程按照CPU分配时间顺序来执行,(t1到t4的执行顺序需参考CPU分配的顺序来定)
*
* ****/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread001 myThread = new MyThread001();
Thread t1 = new Thread(myThread,"t1");
Thread t2 = new Thread(myThread,"t2");
Thread t3 = new Thread(myThread,"t3");
Thread t4 = new Thread(myThread,"t4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}