线程同步
线程同步
并发:同一个对象被多个线程同时操作
现实生活中,我们会遇到"同一个资源,多个人都想使用"的问题,比如食堂排队打饭,每个人都想吃饭,最天然的解决办法就是,排队,一个一个来
处理多线程问题时,多个线程访问同一个对象,并且某些线程还想修改这个对象,这时候我们就需要线程同步,
线程同步其实就是一种等待机制,多个需要同时访问此对象的线程进入这个对象的等待池,形成队列,等待前面线程使用完毕,下一个线程再次使用
队列和锁*
-
由于同一线程的多个线程共享同一块存储空间,在带来方便的同时,也带来了访问冲突问题,为了保证数据在方法中被访问时的正确性,在访问时加入锁机制synchronized,当一个线程获得对象的排他锁,独占资源,其他线程必须等待,使用后释放锁即可,存在以下问题:
-
一个线程持有锁会导致其他所有需要此锁的线程挂起;
-
在多线程竞争下,加锁,释放锁会导致比较多的上下文切换和调度延时,引起性能问题
-
三大不安全案例
package com.hua.syn; //不安全的买票 //线程不安全,有负数 public class UnsafeBuyTicket { public static void main(String[] args) { BuyTicket station = new BuyTicket(); new Thread(station,"你").start(); new Thread(station,"小明").start(); new Thread(station,"黄牛").start(); } } class BuyTicket implements Runnable{ private int ticketNums = 10; boolean flag =true;//外部停止方式 @Override public void run() { while(flag) { buy(); } } public void buy(){ //判断是否有票 if(ticketNums<=0){ flag = false; return; } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } //买票 System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--); } }
package com.hua.syn; //不安全的取钱 //两个人去银行取钱,账户 public class UnsafeBank { public static void main(String[] args) { Account account = new Account(100, "生意基金"); Drawing you = new Drawing(account,50,"你"); Drawing mom = new Drawing(account,100,"mom"); you.start(); mom.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); //Thread.currentThread().getName() 等价于 this.getName(),因为this指代是Drawing,而Drawing拥有Thread的所有方法,也包括getName System.out.println(this.getName()+"手里的钱:"+nowMoney); } }
package com.hua.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(()->{ list.add(Thread.currentThread().getName()); }).start(); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(list.size()); } }
我的一生
第1章-废物的一生
第50章-糟糕的婴儿
第300章-莫欺少年穷
第600章-莫欺中年穷
第1000章-莫欺老年穷
第1100章-不详的离去
第1101章-棺材板的震动
第1150章-盗墓贼的眼泪
第1200章-死者为大