• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

How to allow multiple readers read shared resource

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic