1 Replies - 16608 Views - Last Post: 01 March 2008 - 11:10 PM Rate Topic: -----

#1 naash  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 26-August 07

Readers/Writers problem

Posted 01 March 2008 - 07:31 PM

The problem consists of readers & writers that share a data resource (comman). The readers only wanna read from the resource & writers only wanna write to it. Obviously there isn't
a problem if2 or more readers access the resource simultaneously. However, if a writer & a reader or 2 writers access their resource simultaneously the result becomes indeterminate. Therefore, the writers must have exclusive access to the resource.

I made the following code that well compiles but upon running gives the following output.

java.lang.NoSuchMethodError:main
Exception in thread "main"


Now don't say it runs in URs. Tell me a way out !



public class RWLock
{
	private int givenLocks;
	private int waitingWriters;
	public static boolean TRACE = false;
		
	private Object mutex;
	
	
	public RWLock()
	{
		mutex = new Object();
		givenLocks = 0;
		waitingWriters = 0;
	}
	
	public void getReadLock()
	{
		synchronized(mutex)
		{

			try
			{
				while((givenLocks == -1) || (waitingWriters != 0))
				{
					if(TRACE)
						System.out.println(Thread.currentThread().toString() + "waiting for readlock");
					mutex.wait();
				}
			}
			catch(java.lang.InterruptedException e)
			{
				System.out.println(e);
			}
			
			givenLocks++;
			
			if(TRACE)
				System.out.println(Thread.currentThread().toString() + " got readlock, GivenLocks = " + givenLocks);
			
		}
	}
	
	public void getWriteLock()
	{
		synchronized(mutex)
		{
			waitingWriters++;
			try
			{
				while(givenLocks != 0)
				{
					if(TRACE)
						System.out.println(Thread.currentThread().toString() + "waiting for writelock");
					mutex.wait();
				}
			}
			catch(java.lang.InterruptedException e)
			{
				System.out.println(e);
			}
			
			waitingWriters--;
			givenLocks = -1;
	
			if(TRACE)
				System.out.println(Thread.currentThread().toString() + " got writelock, GivenLocks = " + givenLocks);
		}
	}
	
	
	public void releaseLock()
	{	
		
		synchronized(mutex)
		{

			if(givenLocks == 0)
				return;
				
			if(givenLocks == -1)
				givenLocks = 0;
			else
				givenLocks--;
			
			if(TRACE)
				System.out.println(Thread.currentThread().toString() + " released lock, GivenLocks = " + givenLocks);

			mutex.notifyAll();
		}
	}


}




Is This A Good Question/Topic? 0
  • +

Replies To: Readers/Writers problem

#2 quim  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 182
  • Joined: 11-December 05

Re: Readers/Writers problem

Posted 01 March 2008 - 11:10 PM

You didn't define the main method. Every java program must have a least one
and only one main method.
- The program compile fine, but when java (JRE) runs the firs think it will look
for is the main method and in this program it will thrown the exception since
there is not main.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1