Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Readers Writers problem and Synchronized doubts

968204Oct 14 2012 — edited Oct 14 2012
I made this code for implementing some Lock because I have to permit the write/read action on shared file between different threads. I would like to let more than one thread reading at the same time and just one for the writing

This is my code, it's in italian but in english the class name would be AccessManager

import java.io.Serializable;
public class GestoreAccessi implements Serializable{
//number of reader
private int lettori= 0;
//flag for someone writing
private boolean scrittura= false;
//number of writer waiting
private int scrittoriAttesa = 0;
private String nome;

//Il gestore accessi prende un nome
public GestoreAccessi(String nom) {
nome = nom;
}
//Blocco per la lettura -- Lock for Reading
public synchronized void lockRead() {
try {
//Fino a che c'e' qualcuno in scrittura o processi che aspettano per la scrittura mi metto in attesa -- If ther is some thread writing or waiting for write
while (scrittura || scrittoriAttesa > 0) {
wait();
}
//Altrimenti aumento la variabile dei thread in lettura -- Add one thread reading
++lettori;
} catch (InterruptedException ex) {
}
}
//Fine del processo di lettura -- Finish reading
public synchronized void unlockRead() {
--lettori;
// solo gli scrittori sono in attesa e uno soltanto deve essere svegliato
if (lettori == 0) {
notifyAll();
}
}
//Blocco per la scrittura -- Start Writing
public synchronized void lockWrite() {
++scrittoriAttesa;
//Fino a che ci sono lettori o qualcuno che scrive aspetto -- if there are readers o someone writing
while (lettori > 0 || scrittura) {
try {
wait();
} catch (InterruptedException ex) {
}
}
--scrittoriAttesa;
//Altrimenti inizio il processo di lettura -- Start the process of reading
scrittura = true;
}
//Fine del processo di scrittura
public synchronized void unlockWrite() {
scrittura = false;
notifyAll(); // sblocca uno o piu' lettori o uno scrittore
}
}

Before all critical part I create a GestoreAccessi object and invoce the .lock.. method and finish the critical part the .unlock.. Do you think it's a good way to solve the problem?

I have some doubts on the multiple reading at the same time. I read on synchronized: "First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads."

It's possible to two different thread read at the same time? The .lockRead() method is synchronized: if one thread has acess in a critical part it takes the lock and no other thread would be able to have an access on the same part?! How can i do this? I have not to declare synchronized the read method?

Please help me and sorry for my bad english!!
Bless from Italy
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 11 2012
Added on Oct 14 2012
7 comments
499 views