同一个类中的2个方法都加了同步锁,多个线程能通知访问同一个类中两个方法么

这个问题需要考虑到Lock与synchronized 两种实现锁的不同情形。因为这种情况下使用Lock 和synchronized
会有截然不同的结果。Lock可以让等待锁的线程响应中断,Lock获取锁
使用Lock可以,但使用synchronized不行

public class qq  {
    
    private int count = 0;
    private Lock lock = new ReentrantLock();//设置lock锁
    
    public Runnable run1 = new Runnable(){
         public void run(){
             lock.lock();//加锁
             while(count<1000){
                 try
                 {
                    //打印是否执行了该方法
                    System.out.println(Thread.currentThread().getName()+"run1:"+count++);
                
                 }
                 catch (Exception e)
                 {
                     e.printStackTrace();
                 }
             }
             lock.unlock();
         }
    
    };

    public Runnable  run2 = new Runnable(){
               public void run(){
                   lock.lock();
                   while(count<1000){
                       try
                       {
                        System.out.println(Thread.currentThread().getName()+"run2:"+count++);
                       }
                       catch (Exception e)
                       {
                           e.printStackTrace();
                       }
                   }  
                   lock.unlock();
               }    
    };
    
    public static void main(String[] args) throws InterruptedException
    {
        qq t = new qq();
        new Thread(t.run1).start();
        new Thread(t.run2).start();
    }
}
结果是: 
Thread-0 run1: 0 
Thread-0 run1: 1 
Thread-0 run1: 2 
Thread-0 run1: 3 
Thread-0 run1: 4 
Thread-0 run1: 5 
Thread-0 run1: 6 ........

public class qq  {
    
    private int count = 0;
    private Lock lock = new ReentrantLock();//设置lock锁
    
    public Runnable run1 = new Runnable(){
         public void run(){
        synchronized(this);//将当前类作为锁
             while(count<1000){
                 try
                 {
                    //打印是否执行了该方法
                    System.out.println(Thread.currentThread().getName()+"run1:"+count++);
                
                 }
                 catch (Exception e)
                 {
                     e.printStackTrace();
                 }
             }
        
         }
    
    };

    public Runnable  run2 = new Runnable(){
               public void run(){
                   synchronized(this);//将当前类作为锁
                   while(count<1000){
                       try
                       {
                        System.out.println(Thread.currentThread().getName()+"run2:"+count++);
                       }
                       catch (Exception e)
                       {
                           e.printStackTrace();
                       }
                   }  
                 
               }    
    };
    
    public static void main(String[] args) throws InterruptedException
    {
        qq t = new qq();
        new Thread(t.run1).start();
        new Thread(t.run2).start();
    }
}
结果为: Thread-1 run2: 0 
Thread-1 run2: 1 
Thread-1 run2: 2 
Thread-0 run1: 0 
Thread-0 run1: 4 
Thread-0 run1: 5 
Thread-0 run1: 6     
 ......

你可能感兴趣的:(同一个类中的2个方法都加了同步锁,多个线程能通知访问同一个类中两个方法么)