• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to allow multiple readers read shared resource  RSS feed

 
Deven Juthani
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program which will use threads to show the working of an IPC mechanism for Readers Writers problem
I have following classes
class Reader extends thread
class Writer extend thread
class Resource
int data;
synchronized readme(Reader a)
synchronized writeme(Writer a)

The threads synchronize on the Resource class object. Now I want multiple readers to access the resource but at the same time ensure mutual exclusion among Readers and Writers. How can I achieve that ?
 
sony gam
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
please go through this code which i guess is a solution to what you are asking.
Basically what this does is:
1. Mutual exclusive between reader and writer threads is acheived with the 'flag' object.
2. Before reader or writer can have access to the 'data', they check for 'flag' value. (only if flag = reader, reader threads can proceed, only if flag = writer, writer thread can proceed).
Otherwise they block there, and the one which could proceed and obtain access to the data, will wake the waiting threads. As soon as a reader/writer thread wakes up, they set the falg to its type.
4. The reader threads are allowed to access data simultaneosly.
5. Writer threads can modify data one at a time.

------------------------------------------------------
public class Main
{
private int data = 0;
private String flag = "writer";
public Main()
{
}
public static void main(String[] a)
{
Main m = new Main();
}
class Writer extends Thread
{
private int name;
public Writer(int name)
{
this.name = name;
}
public void run()
{
synchronized(flag)
{
if(flag.equals("reader"))
{
try { flag.wait(); } catch(Exception e) {}
}
flag = "writer";
}
writeData();
synchronized(flag)
{
flag.notifyAll();
}
}
private synchronized void writeData()
{
data+=1;
System.out.println("Writer thread:"+name+" wrote data="+data);
}
}
class Reader extends Thread
{
private int name;
public Reader(int name)
{
this.name = name;
}
public void run()
{
synchronized(flag)
{
if(flag.equals("writer"))
{
try{ flag.wait(); } catch(Exception e) {}
}
flag = "reader";
}
readData();
synchronized(flag)
{
flag.notifyAll();
}
}
private void readData()
{
System.out.println("Data="+data+" from Reader thread:"+name);
}
}
}

------------------------------------------
Hope this helps.
thanks,
Sony Gam
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deven Juthani:
I am writing a program which will use threads to show the working of an IPC mechanism for Readers Writers problem
I have following classes
class Reader extends thread
class Writer extend thread
class Resource
int data;
synchronized readme(Reader a)
synchronized writeme(Writer a)

The threads synchronize on the Resource class object. Now I want multiple readers to access the resource but at the same time ensure mutual exclusion among Readers and Writers. How can I achieve that ?


Are you saying that you are looking for a way to TEST that your synchronization is working? You are trying to get multiple threads to attempt simultaneous access?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!