线程同步

时间:2021-09-07 07:44:07   收藏:0   阅读:25

线程同步

并发:同一个对象被多个线程同时操作

 

同步方法

同步块:

package com.Spp.syn;
?
public class UnSafeBank {
   public static void main(String[] args) {
       Account account = new Account(3600,"备用金");
       Drawing dra1 = new Drawing(account,280,"dra1");
       Drawing dra2 = new Drawing(account,100,"dra2");
       dra1.start();
       dra2.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 takeMoney;
   // 现在手里有多少钱
   int nowMoney;
   public Drawing( Account account,int takeMoney,String name){
       super(name);
       this.account = account;
       this.takeMoney = takeMoney;
  }
?
   @Override
   public void run() {// 取钱
       //锁的对象就是变化的量,需要增删改查的对象
       synchronized (account){
           // 判断钱够不够
           if(account.money-takeMoney<0){
               System.out.println(Thread.currentThread().getName()+"钱不够,取不了这么多");
               return;
          }
           try {
               sleep(100);
          } catch (InterruptedException e) {
               e.printStackTrace();
          }
           account.money = account.money-takeMoney;
           nowMoney = nowMoney+takeMoney;
           System.out.println(account.name+"余额为"+account.money);
           //Thread.currentThread().getName() = this.getName()
           System.out.println(this.getName()+"手里的钱为"+nowMoney);
      }
      }
?
}

 

死锁

执行卡死

package com.kuang.ThreadLesson;
?
public class DeadLock {
   public static void main(String[] args) {
       MakeUp g1 = new MakeUp(0,"11");
       MakeUp g2 = new MakeUp(1,"22");
       g1.start();
       g2.start();
  }
}
//口红
class LipStick{
?
}
//镜子
class Mirror{
?
}
?
class MakeUp extends Thread{
   // 需要的资源只有一份,用static来保证只有一份
   static LipStick lipStick = new LipStick();
   static Mirror mirror = new Mirror();
   int choice;// 选择
   String name;// 使用的人
   MakeUp(int choice,String name){
       this.choice = choice;
       this.name = name;
  }
   @Override
   public void run() {
       try {
           makeup();
      } catch (InterruptedException e) {
           e.printStackTrace();
      }
  }
   private void makeup() throws InterruptedException {
       if(choice == 0){
           synchronized (lipStick){
               System.out.println(this.name+"获得口红的锁");
               Thread.sleep(100);
               synchronized (mirror){
                   System.out.println(this.name+"获得镜子的锁");
              }
          }
?
      }else{
           synchronized (mirror){
               System.out.println(this.name+"获得镜子的锁");
               Thread.sleep(200);
               synchronized (lipStick){
                   System.out.println(this.name+"获得口红的锁");
              }
          }
?
      }
  }
}

顺利执行

 private void makeup() throws InterruptedException {
       if(choice == 0){
           synchronized (lipStick){
               System.out.println(this.name+"获得口红的锁");
               Thread.sleep(100);
          }
           synchronized (mirror){
               System.out.println(this.name+"获得镜子的锁");
          }
      }else{
           synchronized (mirror){
               System.out.println(this.name+"获得镜子的锁");
               Thread.sleep(200);
          }
           synchronized (lipStick){
               System.out.println(this.name+"获得口红的锁");
          }
      }
  }

Lock(锁)

ReentrantLock:可重入锁

ReentrantLock类实现Lock锁

package com.kuang.ThreadLesson;
import java.util.concurrent.locks.ReentrantLock;
public class TestLock {
   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{
   int ticketNumbers = 10;
   //定义Lock锁
   private final ReentrantLock lock = new ReentrantLock();
?
   @Override
   public void run() {
       while (true) {
           try {
               lock.lock();//加锁
               if (ticketNumbers > 0) {
                   try {
                       Thread.sleep(1000);
                  } catch (InterruptedException e) {
                       e.printStackTrace();
                  }
                   System.out.println(ticketNumbers--);
              }
          } finally {
               lock.unlock();//解锁
          }
      }
  }
}
synchronized 与Lock的对比

原文:https://www.cnblogs.com/crushSpp/p/15233811.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!