el método sincronizado no toma int como su argumento válido?
Frecuentes
Visto 2,224 veces
4
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
4 Respuestas
5
You cannot use primitive as an intrinsic lock. Instead, use:
synchronized(Singleton.class)
respondido 27 nov., 13:05
1
this should work as expected...
synchronized(this){
Singleton.setBuffer(Singleton.getBuffer()+1);
notify();
}
respondido 27 nov., 13:05
0
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
0
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 java multithreading or haz tu propia pregunta.
¿Por qué no solo
synchronized(Singleton)
? - Elliott FrischQue es
Singleton.getBuffer()
? Can you show theSingleton
class? BTW, you cannot lock a primitive type. - Rohit Jainwhy not having
synchronized(this)
en lugar de - Ahsan Shah