类中同步方法被调用,其他方法是否可以同时被调用

    毕业初期,面试时经常被问到一个问题:一个类中同步方法被调用时,它的其他方法是否可以同时被调用?记得当时看过面试宝典,答案是否,不能被调用。但是其实这个答案是错误的。
正确的回答应该是该类中其他同步方法不能被同时调用,但非同步方法可以被调用。下面给出例子。
Business.java

package thread;

public class Business
{
    
    public synchronized void methodA() throws InterruptedException
    {
        Thread.sleep(10000);
        System.out.println("I am method A, I am a synchronized method");
    }
    
    public void methodB()
    {
        System.out.println("I am not a synchronized method, check if i can be called when the a synchronized method are called");
    }
    
    public synchronized void methodC()
    {
        System.out.println("I am a synchronized method, check if i can be called when the a synchronized method are called");
    }
}



Thread1.java

package thread;

public class Thread1 implements Runnable
{
    private Business bus;
    public Thread1(Business b)
    {
        this.bus = b;
    }
    @Override
    public void run()
    {
        try
        {
            bus.methodA();
        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}



Thread2.java

package thread;

public class Thread2 implements Runnable
{
    private Business bus;
    public Thread2(Business b)
    {
        this.bus = b;
    }
    @Override
    public void run()
    {
        bus.methodB();
    }

}



thread3.java

package thread;

public class Thread3 implements Runnable
{
    private Business bus;
    public Thread3(Business b)
    {
        this.bus = b;
    }
    @Override
    public void run()
    {
        bus.methodC();
    }

}



TestMain.java

package thread;

public class TestMain
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        Business b = new Business();
        Thread th1 = new Thread(new Thread1(b));
        Thread th2 = new Thread(new Thread2(b));
        Thread th3 = new Thread(new Thread3(b));
        th1.start();
        th2.start();
        th3.start();
    }

}



运行会发现methodB的会立即被执行,但是methodA会sleep 10s,然后跟着methodC执行。

其实很容易理解,用sychronized把methodA和methodC加锁,默认情况下sychronized是对当前class类对象加锁,相当于

    public void methodA() throws InterruptedException
    {
        synchronized(this)
        {
            Thread.sleep(10000);
            System.out.println("I am method A, I am a synchronized method");
        }
       
    }

 public void methodC()
    {
        synchronized(this)
        {
            System.out.println("I am a synchronized method, check if i can be called when the a synchronized method are called");
        }
        
    }


所以当methodA持有锁时methodC就不能拿到锁,故不能被调用,而methodB则不需要获取锁,故可以被执行
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值