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

Java

 
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 
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 
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 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 
AnswerRe: jfreechar tooltip not able to show Pin
Richard MacCutchan5-Dec-19 22:35
mveRichard MacCutchan5-Dec-19 22:35 
QuestionWhat is a medium-size Java open source project with really well maintained automated tests? Pin
Member 146772343-Dec-19 18:49
MemberMember 146772343-Dec-19 18:49 
AnswerRe: What is a medium-size Java open source project with really well maintained automated tests? Pin
Richard MacCutchan3-Dec-19 23:19
mveRichard MacCutchan3-Dec-19 23:19 
GeneralHelp with a website written in Java Pin
Tim Kohl25-Nov-19 23:13
MemberTim Kohl25-Nov-19 23:13 

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.