Click here to Skip to main content
14,427,368 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 
Hi guys, this is my code but I got some error. Anyone can help me?
I have an error in executer part.
public class ReaderWriter{

   public static void main(String args[]){
	   
	   ReadWriteLock RW = new ReadWriteLock();
	   ExecutorService executorService = Executors.newCachedThreadPool();
  
	   executorService.execute(new Writer(RW));
		executorService.execute(new Writer(RW));
		executorService.execute(new Writer(RW));
		executorService.execute(new Writer(RW));
		
		executorService.execute(new Reader(RW));
		executorService.execute(new Reader(RW));
		executorService.execute(new Reader(RW));
		executorService.execute(new Reader(RW));

  }
}
//Writer class
class Writer implements Runnable
{
   private ReadWriteLock rwlock;
   private int writerNum;

    public Writer(int w, ReadWriteLock rw) {
      writerNum = w;
      rwlock = rw;
   }

    public void run() {
      while (true){
         SleepUtilities.nap();
      
         System.out.println("writer " + writerNum + " wants to write.");
         rwlock.WriteLock(writerNum);

         SleepUtilities.nap();
      
         rwlock.WriteUnLock(writerNum);
      }
   }
}

//Reader class
class Reader implements Runnable
{

   private ReadWriteLock rwlock;
   private int readerNum;

    public Reader(int readerNum, ReadWriteLock database) {
      this.readerNum = readerNum;
      this.rwlock = database;
   }

    public void run() {
      while (true) {
         SleepUtilities.nap();
         System.out.println("reader " + readerNum + " wants to read.");
         rwlock.ReadLock(readerNum);
      
      // you have access to read from the database
      // let's read for awhile .....
         SleepUtilities.nap();
      
         rwlock.ReadUnLock(readerNum);
      }
   };
}

//Lock class
class ReadWriteLock {
    private int readerCount;
    private Semaphore mutex; 
    private Semaphore rwlock;     
 
     public ReadWriteLock() {
       readerCount = 0;
       mutex = new Semaphore(1);
       rwlock = new Semaphore(1);
    }
 
     public void ReadLock(int readerNum) {
       try{
          mutex.acquire();
       }
           catch (InterruptedException e) {
        	   
           }
       ++readerCount;
    

       if (readerCount == 1){
          try{
        	  rwlock.acquire();
          }
              catch (InterruptedException e) {
            	  
              }
       }
    
       System.out.println("Reader " + readerNum + " is reading. Reader count = " + readerCount);
       mutex.release();
    }
 
     public void ReadUnLock(int readerNum) {
       try{
          mutex.acquire();
       }
           catch (InterruptedException e) {}
    
       --readerCount;

       if (readerCount == 0){
    	   rwlock.release();
       }
    
       System.out.println("Reader " + readerNum + " is done reading. Reader count = " + readerCount);

       mutex.release();
    }
 
     public void WriteLock(int writerNum) {
       try{
    	   rwlock.acquire();
       }
           catch (InterruptedException e) {}
       System.out.println("Writer " + writerNum + " is writing.");
    }
 
     public void WriteUnLock(int writerNum) {
       System.out.println("Writer " + writerNum + " is done writing.");
       rwlock.release();
    }
 }
class SleepUtilities
{
    public static void nap() {
      nap(NAP_TIME);
   }

    public static void nap(int duration) {
      int sleeptime = (int) (NAP_TIME * Math.random() );
      try { RW.sleep(sleeptime*1000); }
          catch (InterruptedException e) {}
   }

   private static final int NAP_TIME = 5;
}

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 
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.