简体   繁体   中英

Synchronized method in Java monitor

I am currently learning the use of monitor in Java, but i do not know how the synchronized methods work.

I understand that while one thread is inside a synchronized method, another thread cannot be inside a synchronized method and that sleep doesn't take off the monitor's own ownership.

So i tried to write a code to test that

import java.util.Random;
public class ex3 extends Thread {

private static int nbA=0;
private static int nbB=0;
public static final Random rand = new Random();

public void run(){
    while(true){
        System.out.println(nbA+" "+nbB);
        try{
            Thread.sleep(rand.nextInt(500));
        }catch (Exception e ){e.printStackTrace();}
        if (rand.nextBoolean()){
            try {
                A();
            } catch (InterruptedException e) {}
        }else{
            try {
                B();
            } catch (InterruptedException e) {}
        }
    }
}

public synchronized void A() throws InterruptedException{
    nbA++;
    Thread.sleep(rand.nextInt(500));
    nbA--;
}

public synchronized void B() throws InterruptedException{   
    nbB++;
    Thread.sleep(rand.nextInt(500));
    nbB--;
}

public static void main(String[] argv){
    new ex3().start();
    new ex3().start();
    new ex3().start();
}
}

I believed it was impossible that nbA or nbB be superior to 1 or that nbB and nbA are both >0 but it's happening

What do I misunderstand ?

Sorry for the bad english.

You're synchronizing on different objects: a synchronized non-static method synchronizes on this , so each of the new ex3() instances effectively works like it's not synchronized.

A synchronized instance method is exactly equivalent to this:

public void A() {
    synchronized (this) {
        // The body.
    }
}

Either make the synchronized methods static , or explicitly synchronize on the class (or something other shared object):

public void A() throws InterruptedException{
    synchronized (ex3.class) {
        nbA++;
        Thread.sleep(rand.nextInt(500));
        nbA--;
    }
}

I understand that while one thread is inside a synchronized method, another thread cannot be inside a synchronized method

Wrong. It cannot be inside a synchronized method synchronized on the same object. It can be inside any other synchronized method, or the same method synchronized on a different object, as here.

and that sleep doesn't take off the monitor's own ownership.

Correct.

NB Per Brinch Hansen doesn't consider Java to have monitors, and he invented them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM