Java – can two threads access synchronous methods at the same time?

public class Deadlock {
public class Deadlock {
    static class Friend {
        private final String name;
        public Friend(String name) {
            this.name = name;
        }
        public String getName() {
            return this.name;
        }
        public synchronized void bow(Friend bower) {
            System.out.format("%s: %s"
                + "  has bowed to me!%n",this.name,bower.getName());
            bower.bowBack(this);
        }
        public synchronized void bowBack(Friend bower) {
            System.out.format("%s: %s"
                + " has bowed back to me!%n",bower.getName());
        }
    }

    public static void main(String[] args) {
        final Friend alphonse =
            new Friend("Alphonse");
        final Friend gaston =
            new Friend("Gaston");
        new Thread(new Runnable() {
            public void run() { alphonse.bow(gaston); }
        }).start();
        new Thread(new Runnable() {
            public void run() { gaston.bow(alphonse); }
        }).start();
    }
}

When I run this program, I get the output

A Fang: Gaston bows to me Gaston: a Jian bows to me

Can two threads access the synchronization method at the same time?

Solution

Instance methods, such as your example, synchronize on the object that contains them In this case, when you call Alphonse When bow (...), you lock the Alphonse object gaston. Bow (...) lock Gaston

There are several ways to lock multiple instances of an object to the same object

>You can make the methods static and synchronous, in which case they will be locked to the class object itself Each class loader has only one of these objects

public static synchronized void bow(Friend bower) {

>They can lock a defined static object It's like:

private static final Object lockObject = new Object();
...
public void bow(Friend bower) {
    synchronized (lockObject) {
        ....
    }
}

>Or you can pass an object to lock if you don't want it to be static

Your output might look like the following:

>The Gaston thread (possibly) starts and calls bow (Alphonse) > this locks the Gaston object and outputs: Gaston: Alphonse has bowed to me! It calls Alphonse bowBack(this). > This call locks the Alphonse object and outputs: Alphonse: Gaston has returned to me! > alphonse. Bowback (this) exits and unlocks the Alphonse object. > gaston. Bow (Alphonse) exit to unlock the Gaston object. > Then the Gaston thread exits. > The Alphonse thread (possibly) starts next and calls bow (Gaston) > this locks the Alphonse object and outputs: Alphonse: Gaston has bowed to me! It's called Gaston Bowback. > This call locks the Gaston object and outputs: Gaston: Alphonse has returned to me! > gaston. Bowback (this) exits and unlocks the Gaston object. > alphonse. Bow (Gaston) exits and unlocks the object

This can happen on many different orders Even if the start () method is called later, you can run the Alphonse thread first. If Alphonse Bowback (...) is currently running, and the only way to lock save is to call Alphonse bow(…). As @ user988052 pointed out, because each thread locks its own object and then tries to lock another, you can easily get a deadlock

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>