Thread线程

synchronized和Lock的区别

1.synchronized是内置的java关键字,Lock是java类
2.synchronized无法判断获取锁的状态,Lock可以判断获取锁的状 态。
3.synchronized会自动释放锁,lock要手动释放,如果不释放就会死 锁。
4.synchronized线程1(获得锁、阻塞)、线程2(等待、傻傻等待);lock锁不一定会等待下去。 lock.tryLock()尝试获取锁
5. synchronized适合少量代码同步,Lock适合大量代码同步。
(Lock灵活度搞)

**一:synchronized(传统方式)

public class TestSell {
    public static void main(String[] args) {
        Ticket ticket=new Ticket();

        new Thread(new Runnable() {
            @Override
            public void run() {
                ticket.sell();
            }
        },"111").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20000; i++) {
                    ticket.sell();
                }
            }
        },"111").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20000; i++) {
                    ticket.sell();
                }
            }
        },"222").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20000; i++) {
                    ticket.sell();
                }
            }
        },"333").start();


//  new Thread(()->{
//      for (int i = 0; i < 12000; i++) {
//          ticket.sell();
//      }
//  },"AA").start();
//
//        new Thread(()->{
//            for (int i = 0; i < 12000; i++) {
//                ticket.sell();
//            }
//        },"bb").start();
//
//        new Thread(()->{
//            for (int i = 0; i < 12000; i++) {
//                ticket.sell();
//            }
//        },"cc").start();

    }
}
class Ticket{
    private int count =10000;
    public synchronized void sell(){
        if (count>0){
            System.out.println(Thread.currentThread().getName()+"买了第"+count+"张票==========");
            count--;
        }
    }
}

一:synchronized(armda表达式方式)**

public class TestSell {
    public static void main(String[] args) {
        Ticket ticket=new Ticket();
  new Thread(()->{
      for (int i = 0; i < 12000; i++) {
          ticket.sell();
      }
  },"AA").start();

        new Thread(()->{
            for (int i = 0; i < 12000; i++) {
                ticket.sell();
            }
        },"bb").start();

        new Thread(()->{
            for (int i = 0; i < 12000; i++) {
                ticket.sell();
            }
        },"cc").start();

    }
}
class Ticket{
    private int count =10000;
    public synchronized void sell(){
        if (count>0){
            System.out.println(Thread.currentThread().getName()+"买了第"+count+"张票==========");
            count--;
        }
    }
}

二:Lock锁

public class LockTest {
    public static void main(String[] args) {

        Ticket ticket = new Ticket();
        new Thread(() -> { for (int i = 0; i < 12000; i++) ticket.sell(); }, "111").start();
        new Thread(() -> { for (int i = 0; i < 12000; i++) ticket.sell(); }, "222").start();
        new Thread(() -> { for (int i = 0; i < 12000; i++) ticket.sell(); }, "333").start();

    }
}
    class Ticket{
        private int count =10000;
        Lock lock=new ReentrantLock();

        public  void sell(){
            lock.lock();
            try {
                if (count>0) System.out.println(Thread.currentThread().getName()+"买了第"+count+"张票==========");
                count--;
            }
            finally {
                lock.unlock();
            }
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值