Sign in to follow this  
Warhorseii

Multi-Threading problem in Java....

Recommended Posts

Ive been working on this all day and I cannot seem to figure it out... any help would be appreciated. These are the rules I must follow... Readers can read the database concurrently. Writers need exclusive access to the database. Two writers cannot have concurrent access to the database. A reader and a writer cannot have concurrent access to the database. When no writer is active, readers must be permitted to access the database concurrently. At the beginning of the reader Class startRead() is called and at the endRead() is called. The same goes for the Writer class. volatile public int accessType = 0; // 0 = No Access // 1 = Reader Accessing // 2 = Writer Accessing volatile public int readers = 0; synchronized public void startRead() throws InterruptedException { if (accessType == 2) { try { wait(); } catch (InterruptedException e) { System.err.println("interrupted out of wait"); } accessType = 1; readers++; } else if (accessType == 1) readers++; else if (accessType == 0) { accessType = 1; readers++; } } synchronized public void endRead() { if (readers == 1) { readers--; accessType = 0; notifyAll(); } else readers--; } synchronized public void startWrite() throws InterruptedException { if (accessType == 1 || accessType == 2) { try { wait(); } catch (InterruptedException e) { System.err.println("interrupted out of wait"); } accessType = 2; } else // if (accessed == true) { accessType = 2; } } synchronized public void endWrite() { accessType = 0; notifyAll(); } For one reason or another threads are not getting blocked from accessing the data... Any help would be appreciated

Share this post


Link to post
Share on other sites
Hi,
I have a few comments on your java code:
You can replace the notifyAll() with just notify() since its expensive to notify all the threads, only 1 waiting thread should be waken. I have a textbook discussing the same problem you're facing, the reader writer problem. But since your code looks ok, I'll give you some other suggestions:

Ok, what I think you should try is everywhere you use the wait() func, rewrite it like so:
From this:

if (accessType == 1 || accessType == 2)
{
try
{
wait();
}
catch (InterruptedException e)
{
System.err.println("interrupted out of wait");
}

accessType = 2;
}




To this:

while (accessType == 1 || accessType == 2)
{
try
{
wait();

accessType = 2;

break;
}
catch (InterruptedException e)
{
System.err.println("interrupted out of wait");
}
}




I guess that should help it to work.

Maybe you can use a boolean dbWriting variable indicating when the database is being written to, instead of having accessType variables.

I don't mean to be a critic or anything, I'm just giving some suggestions which you can try out. Sorry if I look intimidating, but I'm not trying to be.

Let me know if any of this works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this