码迷,mamicode.com
首页 > 其他好文 > 详细

ReentrantLock和synchronized

时间:2021-05-03 11:52:44      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:rup   except   final   dex   finally   seconds   new   email   sleep   

package com.atguigu.boot.com.atguigu;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Phone implements Runnable{
    public synchronized void sendMsg() throws Exception{
        System.out.println(Thread.currentThread().getName()+" 进入 invoked sendMsg()方法");
        sendEmail();
    }
    public synchronized void sendEmail() throws Exception{
        System.out.println(Thread.currentThread().getName()+" 进入 KKKinvoked sendEmail()方法");
    }

    @Override
    public void run() {
       get();
    }

    Lock lock=new ReentrantLock();
    public void get(){
        try{
            lock.lock();
            //lock.lock();
            System.out.println(Thread.currentThread().getName()+" 进入 KKKinvoked get()方法");
            set();
            sendMsg();
        }catch(Exception io){
        } finally{
            lock.unlock();
            //lock.unlock();
        }
    }
    public void set(){
        try{
            lock.lock();
            System.out.println(Thread.currentThread().getName()+" 进入 KKKinvoked set()方法");
        }finally {
            lock.unlock();
        }
    }
}

public class ReenterLockDemo {

    public static void main(String[] args) {

        Phone phone=new Phone();
        new Thread(()->{
            try {
                phone.sendMsg();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->{
            try {
                phone.sendMsg();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"t2").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println();
        Thread thread=new Thread(phone,"t3");
        Thread threads=new Thread(phone,"t4");
        thread.start();
        threads.start();
    }

}

  

ReentrantLock和synchronized

标签:rup   except   final   dex   finally   seconds   new   email   sleep   

原文地址:https://www.cnblogs.com/ffzzcommsoft/p/14722356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!