大家好,我是你的好朋友思创斯。今天说一说java多线程 同步_java 异步线程,希望您对编程的造诣更进一步.
//不安全的买票
//线程不安全,有负数或者多人买到同一张票
public class unsafebuyticket {
public static void main(string[] args) {
buyticket buyticket = new buyticket();
new thread(buyticket,"苦逼的我").start();
new thread(buyticket,"牛逼的你们").start();
new thread(buyticket,"可恶的黄牛党").start();
}
}
class buyticket implements runnable{
//票数
private int num = 10;
private boolean flag = true;//判断是否停止线程 thread的stop已经废弃不建议使用
@override
public void run() {
//买票
while (flag){
buy();
}
}
public void buy(){
//判断是否有票
if(num<=0){
flag = false;
return ;
}
try {
//模拟延时(放大问题的发生性)
thread.sleep(100);
} catch (interruptedexception e) {
e.printstacktrace();
}
//买票
system.out.println(thread.currentthread().getname()"买到了第"num--"票");
}
}
是否还在为ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!
【正版授权,激活自己账号】:
【官方授权 正版激活】:
解决方法:通过关键字synchronized来设置同步锁 在buy()方法前面添加synchronized关键字来锁定这个方法
public synchronized void buy(){
//判断是否有票
if(num<=0){
flag = false;
return ;
}
try {
//模拟延时(放大问题的发生性)
thread.sleep(100);
} catch (interruptedexception e) {
e.printstacktrace();
}
//买票
system.out.println(thread.currentthread().getname()"买到了第"num--"票");
}
如上图添加了synchronized关键字后就避免了多人抢到同一张票的情况,也避免了在没有票的情况下继续出票的情况
//不安全的取钱
//两个人去银行取钱,账户
public class unsafebank {
public static void main(string[] args) {
account account = new account(100, "结婚基金");
drawing you = new drawing(account, 50, "你");
drawing grilfriend = new drawing(account, 100, "grilfriend");
grilfriend.start();
you.start();
}
}
//账户
class account{
int money;//余额
string name;//卡名称
public account(int money, string name) {
this.money = money;
this.name = name;
}
}
//银行:模拟取钱
class drawing extends thread{
account account;//账户
int drawingmoney;//取多少钱
int nowmoney;//现在手里有多少钱
public drawing(account account,int drawingmoney,string name){
super(name);
this.account = account;
this.drawingmoney = drawingmoney;
}
@override
public void run() {
//判断账户里面有没有钱
if(account.money-drawingmoney<0){
system.out.println(thread.currentthread().getname()"钱不够了,取不了");
return;
}
//sleep 放大问题的发生性
try {
thread.sleep(1000);
} catch (interruptedexception e) {
e.printstacktrace();
}
//卡内余额 = 余额 - 你取的钱
account.money = account.money - drawingmoney;
//你手上的钱
nowmoney = nowmoney drawingmoney;
system.out.println(account.name "余额为:"account.money);
//hread.currentthread().getname() = this.getname();
system.out.println(this.getname()"手里的钱"nowmoney);
}
}
上述的问题就是你去50万,你老婆去100万,结果银行还倒贴了50万
解决方法:给银行账户添加锁 synchronized(account)
@override
public void run() {
synchronized (account){
//判断账户里面有没有钱
if(account.money-drawingmoney<0){
system.out.println(thread.currentthread().getname()"钱不够了,取不了");
return;
}
//sleep 放大问题的发生性
try {
thread.sleep(1000);
} catch (interruptedexception e) {
e.printstacktrace();
}
//卡内余额 = 余额 - 你取的钱
account.money = account.money - drawingmoney;
//你手上的钱
nowmoney = nowmoney drawingmoney;
system.out.println(account.name "余额为:"account.money);
//hread.currentthread().getname() = this.getname();
system.out.println(this.getname()"手里的钱"nowmoney);
}
}
只要有一个人取完后另一个人就会因为余额不足取不了,保证了线程的安全
文章由思创斯整理,转载请注明出处:https://ispacesoft.com/141799.html