el método sincronizado no toma int como su argumento válido?

public void run(){
        if(Singleton.getBuffer()<Singleton.getLimit()){
            synchronized(Singleton.getBuffer()){
                    Singleton.setBuffer(Singleton.getBuffer()+1);
                    notify();
            }
        }

In this code i am having trouble getting the lock on statc volatile int buffer.. it says int is not a valid type's statement for a sychronized method

preguntado el 27 de noviembre de 13 a las 05:11

¿Por qué no solo synchronized(Singleton)? -

Que es Singleton.getBuffer()? Can you show the Singleton class? BTW, you cannot lock a primitive type. -

why not having synchronized(this) en lugar de -

4 Respuestas

You cannot use primitive as an intrinsic lock. Instead, use:

synchronized(Singleton.class)

respondido 27 nov., 13:05

this should work as expected...

    synchronized(this){
            Singleton.setBuffer(Singleton.getBuffer()+1);
            notify();
    }

respondido 27 nov., 13:05

Sincronizado statements must specify the object that provides the intrinsic lock. You cannot use primitive types to lock on synchronized statements.

Leer Bloqueos intrínsecos y sincronización document which explains it pretty well.

respondido 27 nov., 13:08

For having a lock you need an object so you can not use primitive types for synchronized block. In your case if you want static/global lock you can use following:

synchronized(String.valueOf(i).intern()){
}

respondido 27 nov., 13:11

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.