Click here to Skip to main content
14,427,367 members
   

Java

 
PinnedForum Guidelines - PLEASE READ PinPopular
Chris Maunder28-Jul-09 3:36
cofounderChris Maunder28-Jul-09 3:36 
QuestionMessage Closed Pin
20-Jan-20 15:21
Memberbekagaw65320-Jan-20 15:21 
AnswerMessage Closed Pin
20-Jan-20 15:22
Memberbekagaw65320-Jan-20 15:22 
JokeJ Pin
Member 1471420112-Jan-20 10:03
MemberMember 1471420112-Jan-20 10:03 
SuggestionRe: Java task zigzag Pin
Richard MacCutchan12-Jan-20 23:11
communityengineerRichard MacCutchan12-Jan-20 23:11 
AnswerRe: Java task zigzag Pin
ZurdoDev13-Jan-20 2:05
mveZurdoDev13-Jan-20 2:05 
GeneralRe: Java task zigzag Pin
Richard MacCutchan13-Jan-20 2:33
communityengineerRichard MacCutchan13-Jan-20 2:33 
Questionbus reservation and ticketting system Pin
Member 1469715023-Dec-19 11:20
MemberMember 1469715023-Dec-19 11:20 
AnswerRe: bus reservation and ticketting system Pin
Richard MacCutchan23-Dec-19 22:32
communityengineerRichard MacCutchan23-Dec-19 22:32 
AnswerRe: bus reservation and ticketting system Pin
ZurdoDev13-Jan-20 2:06
mveZurdoDev13-Jan-20 2:06 
Questionproject on java Pin
Member 1469715023-Dec-19 3:53
MemberMember 1469715023-Dec-19 3:53 
AnswerRe: project on java Pin
Richard MacCutchan23-Dec-19 4:20
communityengineerRichard MacCutchan23-Dec-19 4:20 
AnswerMessage Closed Pin
17-Jan-20 0:57
MemberMember 1471908117-Jan-20 0:57 
QuestionReaderWriter Problem Pin
pembepanter21-Dec-19 11:38
Memberpembepanter21-Dec-19 11:38 
QuestionRe: ReaderWriter Problem Pin
Richard MacCutchan21-Dec-19 22:38
communityengineerRichard MacCutchan21-Dec-19 22:38 
AnswerRe: ReaderWriter Problem Pin
pembepanter22-Dec-19 0:17
Memberpembepanter22-Dec-19 0:17 
GeneralRe: ReaderWriter Problem Pin
phil.o22-Dec-19 4:43
mvephil.o22-Dec-19 4:43 
GeneralRe: ReaderWriter Problem Pin
pembepanter22-Dec-19 5:15
Memberpembepanter22-Dec-19 5:15 
I rewrited main part like this and it worked. But I want to write it with using executer services. But in that way still it is not working.
public class ReaderWriter{
		   public static final int NUM_OF_READERS = 3;
		   public static final int NUM_OF_WRITERS = 2;

		    public static void main(String args[]){
		 	   ReadWriteLock rwlock = new ReadWriteLock();
		   
		      Thread[] readerArray = new Thread[NUM_OF_READERS];
		      Thread[] writerArray = new Thread[NUM_OF_WRITERS];
		   
		      for (int i = 0; i < NUM_OF_READERS; i++) {
		         readerArray[i] = new Thread(new Reader(rwlock));
		         readerArray[i].start();
		      }
		   
		      for (int i = 0; i < NUM_OF_WRITERS; i++) {
		         writerArray[i] = new Thread(new Writer(rwlock));
		         writerArray[i].start();
		      }
		   }

Also like you said I wrote my functions which take just one argument and removed SleepUtlization class.
class Reader implements Runnable
{

	private static int readers = 0;
   private ReadWriteLock rwlock;
   private int readerNum;

    public Reader(ReadWriteLock rwlock) {
      this.rwlock = rwlock;
      this.readerNum=Reader.readers++;
   }

	public void run() {
      while (true) {
    	  final int DELAY = 5000;
          try
          {
            Thread.sleep((int) (Math.random() * DELAY));
          }
          catch (InterruptedException e) { 
          }
         System.out.println("reader " + readerNum + " wants to read.");
         rwlock.ReadLock(readerNum);
      // you have access to read from the database
      // let's read for awhile .....
         try
         {
           Thread.sleep((int) (Math.random() * DELAY));
         }
         catch (InterruptedException e) {
         }
         rwlock.ReadUnLock(readerNum);
      }
   };
}

GeneralRe: ReaderWriter Problem Pin
Richard MacCutchan22-Dec-19 6:02
communityengineerRichard MacCutchan22-Dec-19 6:02 
GeneralRe: ReaderWriter Problem Pin
pembepanter23-Dec-19 2:26
Memberpembepanter23-Dec-19 2:26 
GeneralRe: ReaderWriter Problem Pin
Richard MacCutchan23-Dec-19 3:08
communityengineerRichard MacCutchan23-Dec-19 3:08 
AnswerRe: ReaderWriter Problem Pin
Gerry Schmitz22-Dec-19 4:20
mveGerry Schmitz22-Dec-19 4:20 
GeneralRe: ReaderWriter Problem Pin
pembepanter22-Dec-19 5:16
Memberpembepanter22-Dec-19 5:16 
QuestionJava reader writer project Pin
pembepanter10-Dec-19 23:28
Memberpembepanter10-Dec-19 23:28 
AnswerRe: Java reader writer project Pin
Richard MacCutchan11-Dec-19 0:04
communityengineerRichard MacCutchan11-Dec-19 0:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.