Lock、synchronized、sleep和wait

本文深入探讨了Java中Lock和synchronized关键字的区别,并通过实例代码详细解释了sleep与wait方法的不同之处,展示了如何利用wait和notify实现线程间的交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Lock和synchronized区别:
    Lock能提供更多API,如trylock():返回true表示拿到锁,false表示没有拿到锁;trylock(long time, TimeUnit unit):定义超时时间,避免死锁;unlock():上锁后要在finally中调用unlock()去掉锁。

  • sleep和wait的区别:两者对线程的占用不同,看下面的代码:

public class Consumer implements Runnable{

    public void run() {
        // TODO Auto-generated method stub
        int count = 10;
        while(count > 0) {
             synchronized (Test. obj) {

                 System. out.print( "Consumer");
                 count --;
                 //Test. obj.notify();

                  try {
                       Test. obj.wait();

                 } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                       e.printStackTrace();
                 }
            }
       }
    }
}

public class Produce implements Runnable{

    public void run() {
        // TODO Auto-generated method stub
        int count = 10;
        while(count > 0) {
             synchronized (Test. obj) {

                  //System.out.print("count = " + count);
                 System. out.print( "Produce");
                 count --;
                 //Test. obj.notify();

                  try {
                       Test. obj.wait();
                 } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                       e.printStackTrace();
                 }
            }
        }
    }
}

public class Test {
    public static final Object obj = new Object();

    public static void main(String[] args) {

           new Thread( new Produce()).start();
           new Thread( new Consumer()).start();

    }
}

produce那个runnable先执行,之后wait,此时会释放obj的锁,然后Consumer拿到obj的锁,开始执行,最后结果:

ProduceConsumer

执行到Consumer就结束了,其实程序这时还在运行中,只是处于挂起状态,下面再在Consumer中加入notify:

public class Consumer implements Runnable{

    public void run() {
        // TODO Auto-generated method stub
        int count = 10;
        while(count > 0) {
             synchronized (Test. obj) {

                 System. out.print( "Consumer");
                 count --;
                 //挂起前释放锁
                 Test. obj.notify();

                  try {
                       Test. obj.wait();

                 } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                       e.printStackTrace();
                 }
            }
       }
    }
}

运行结果:
ProduceConsumerProduce
说明Consumer释放了对象锁,Produce重新拿到锁,那么为了让这两个线程轮流执行,每次wait前释放对象锁,即在wait前,调用notify:

public class Produce implements Runnable{

    public void run() {
        // TODO Auto-generated method stub
        int count = 10;
        while(count > 0) {
             synchronized (Test. obj) {

                  //System.out.print("count = " + count);
                 System. out.print( "Produce");
                 count --;
                 Test.obj.notify();

                  try {
                       Test. obj.wait();
                 } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                       e.printStackTrace();
                 }
            }
        }
    }
}

public class Consumer implements Runnable{

    public void run() {
        // TODO Auto-generated method stub
        int count = 10;
        while(count > 0) {
             synchronized (Test. obj) {

                 System. out.print( "Consumer");
                 count --;
                 Test. obj.notify();

                  try {
                       Test. obj.wait();

                 } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                       e.printStackTrace();
                 }
            }
       }
    }
}

结果:
ProduceConsumerProduceConsumerProduceConsumerProduceConsumerProduceConsumerProduceConsumerProduceConsumerProduceConsumerProduceConsumerProduceConsumer

sleep是线程的方法,线程调用后会让出cpu时间片,但并不能操作对象释放对象锁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值