Results 1 to 1 of 1
- 10-14-2012, 12:25 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
Readers Writers problem and Synchronized doubts
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
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?Java Code: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 } }
I have some doubts on the multiple reading at the same time. I read on synchronized: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?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.
Please help me and sorry for my bad english!!
Bless from Italy
Similar Threads
-
Java Writers Neeed, Online Job (15-May-2012)
By kks_krishna in forum Jobs OfferedReplies: 0Last Post: 05-15-2012, 09:24 AM -
Mac Java writers: see if you can improve this instruction set
By java beginner in forum New To JavaReplies: 2Last Post: 05-07-2011, 06:02 AM -
Few Doubts
By sunnyboy in forum New To JavaReplies: 0Last Post: 03-10-2011, 01:55 AM -
scjp doubts
By vijay24805 in forum Java CertificationReplies: 0Last Post: 03-24-2009, 08:27 PM -
Applet doubts
By anithababu in forum Java AppletsReplies: 0Last Post: 01-18-2008, 05:09 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks