简体   繁体   English

Java监视器中的同步方法

[英]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. 我目前正在学习Java中Monitor的用法,但是我不知道同步方法如何工作。

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 我认为nbA或nbB不可能优于1或nbB和nbA都> 0都是不可能的,但是这种情况正在发生

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. 您正在不同的对象上进行synchronizedsynchronized非静态方法this同步,因此每个new ex3()实例都可以像未同步一样有效地工作。

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): 将同步方法static ,或在类(或其他共享对象)上显式同步:

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. NB Per Brinch Hansen不认为Java具有监视器,而是他发明了它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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