线程同步:synchronized及其原理
线程竞争问题
public class ThreadSafeTest {
public static class People implements Runnable {
private int withdrawMoney = 0;
public static Integer money = 1000;
public People(int withdrawMoney) {
this.withdrawMoney = withdrawMoney;
}
@Override
public void run() {
if (money - withdrawMoney >= 0) {
money = money - withdrawMoney;
System.out.println("取钱" + withdrawMoney + "成功,余额为" + money);
} else {
System.out.println("取钱" + withdrawMoney + "失败,余额为" + money);
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new People(200));
Thread t2 = new Thread(new People(400));
Thread t3 = new Thread(new People(300));
Thread t4 = new Thread(new People(100));
Thread t5 = new Thread(new People(200));
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
锁的概念
synchronized
锁释放
wait、notify、notifyAll
锁原理


参考资源
最后更新于