标签:http new alt with 操作 over ++ 资源 nts
来源:https://www.bilibili.com/video/BV1V4411p7EF?p=20
感谢老师!
package com.hao.syn;
//三个线程操作同一个资源
//线程不安全,出现负数。。。sleep放大不安全情况
public class UnsafeBuyTickets {
public static void main(String[] args) {
BuyTickets buyTickets = new BuyTickets();
new Thread(buyTickets,"苦逼的我-----").start();
new Thread(buyTickets,"牛逼的你-----").start();
new Thread(buyTickets,"黄牛党-----").start();
}
}
class BuyTickets implements Runnable{
//票
private int TicketNums = 10;
boolean flag = true;//外部停止方式
@Override
public void run() {
while (flag){
try {
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//synchronized 同步方法
//买票
public synchronized void buy() throws InterruptedException {
//判断是否有票
if (TicketNums <= 0){
flag = false;
return;
}
//模拟延时
Thread.sleep(100);
//买票
System.out.println(Thread.currentThread().getName()+"------->拿到了第"+TicketNums--+"张票");
}
}
package com.hao.syn;
//俩人同时银行取钱
public class UnsafeBank {
public static void main(String[] args) {
Account account = new Account(50,"结婚基金");
WithDrawMoney you = new WithDrawMoney(account,50,"你");
WithDrawMoney wife = new WithDrawMoney(account,100,"你wife");
you.start();
wife.start();
}
}
//账户
class Account{
int money;//余额
String name;
public Account(int money, String name) {
this.money = money;
this.name = name;
}
}
//银行:模拟取款
class WithDrawMoney extends Thread{
Account account;//账户
int withdrawMoney ;//取了多少钱
int nowMoney;//还剩多少钱
public WithDrawMoney(Account account,int withdrawMoney,String name){
super(name);//线程名字
this.account = account;
this.withdrawMoney = withdrawMoney;
}
//取钱
@Override
public void run() {
//synchronized 默认锁的是this,同步块
synchronized (account) {
//判断有没有钱
if (account.money - withdrawMoney < 0) {
System.out.println(Thread.currentThread().getName() + "取的时候,提示钱不够,取不了");
return;
}
//sleep
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
account.money -= withdrawMoney;
nowMoney += withdrawMoney;
System.out.println(account.name + "余额为:" + account.money);
System.out.println(Thread.currentThread().getName() + "手里有" + nowMoney);
}
}
}
package com.hao.syn;
import java.util.ArrayList;
import java.util.List;
//线程不安全集合
public class UnsafeList {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
new Thread(()->{
//synchronized,锁的对象是增删改的对象
synchronized (list) {
list.add(Thread.currentThread().getName());
}
}).start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}
标签:http new alt with 操作 over ++ 资源 nts
原文地址:https://www.cnblogs.com/haohaoxuexio/p/14397991.html