Click here to Skip to main content
14,971,754 members
Home / Discussions / Java
   

Java

 
GeneralMessage Closed Pin
25-Jan-20 5:54
MemberMember 1472651425-Jan-20 5:54 
Questionbus reservation and ticketting system Pin
Member 1469715023-Dec-19 10:20
MemberMember 1469715023-Dec-19 10:20 
AnswerRe: bus reservation and ticketting system Pin
Richard MacCutchan23-Dec-19 21:32
mveRichard MacCutchan23-Dec-19 21:32 
AnswerRe: bus reservation and ticketting system Pin
ZurdoDev13-Jan-20 1:06
professionalZurdoDev13-Jan-20 1:06 
Questionproject on java Pin
Member 1469715023-Dec-19 2:53
MemberMember 1469715023-Dec-19 2:53 
AnswerRe: project on java Pin
Richard MacCutchan23-Dec-19 3:20
mveRichard MacCutchan23-Dec-19 3:20 
AnswerMessage Closed Pin
16-Jan-20 23:57
MemberMember 1471908116-Jan-20 23:57 
QuestionReaderWriter Problem Pin
pembepanter21-Dec-19 10:38
Memberpembepanter21-Dec-19 10: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 21:38
mveRichard MacCutchan21-Dec-19 21:38 
AnswerRe: ReaderWriter Problem Pin
pembepanter21-Dec-19 23:17
Memberpembepanter21-Dec-19 23:17 
GeneralRe: ReaderWriter Problem Pin
phil.o22-Dec-19 3:43
mvephil.o22-Dec-19 3:43 
GeneralRe: ReaderWriter Problem Pin
pembepanter22-Dec-19 4:15
Memberpembepanter22-Dec-19 4:15 
GeneralRe: ReaderWriter Problem Pin
Richard MacCutchan22-Dec-19 5:02
mveRichard MacCutchan22-Dec-19 5:02 
GeneralRe: ReaderWriter Problem Pin
pembepanter23-Dec-19 1:26
Memberpembepanter23-Dec-19 1:26 
GeneralRe: ReaderWriter Problem Pin
Richard MacCutchan23-Dec-19 2:08
mveRichard MacCutchan23-Dec-19 2:08 
GeneralRe: ReaderWriter Problem Pin
Member 1472682625-Jan-20 22:13
MemberMember 1472682625-Jan-20 22:13 
AnswerRe: ReaderWriter Problem Pin
Gerry Schmitz22-Dec-19 3:20
mveGerry Schmitz22-Dec-19 3:20 
GeneralRe: ReaderWriter Problem Pin
pembepanter22-Dec-19 4:16
Memberpembepanter22-Dec-19 4:16 
QuestionJava reader writer project Pin
pembepanter10-Dec-19 22:28
Memberpembepanter10-Dec-19 22:28 
AnswerRe: Java reader writer project Pin
Richard MacCutchan10-Dec-19 23:04
mveRichard MacCutchan10-Dec-19 23:04 
AnswerMessage Closed Pin
16-Dec-19 18:38
MemberMember 1468932316-Dec-19 18:38 
GeneralRe: Java reader writer project Pin
Richard MacCutchan16-Dec-19 23:31
mveRichard MacCutchan16-Dec-19 23:31 
GeneralRe: Java reader writer project Pin
phil.o16-Dec-19 23:57
mvephil.o16-Dec-19 23:57 
GeneralRe: Java reader writer project Pin
Richard MacCutchan17-Dec-19 1:46
mveRichard MacCutchan17-Dec-19 1:46 
Questionjfreechar tooltip not able to show Pin
Member 23212935-Dec-19 16:42
MemberMember 23212935-Dec-19 16:42 

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.