Click here to Skip to main content
15,921,716 members
Home / Discussions / Java
   

Java

 
GeneralRe: Transfer data from SQL database to populate spinner Pin
Member 1475169121-Feb-20 7:38
Member 1475169121-Feb-20 7:38 
GeneralRe: Transfer data from SQL database to populate spinner Pin
Richard MacCutchan21-Feb-20 22:08
mveRichard MacCutchan21-Feb-20 22:08 
GeneralRe: Transfer data from SQL database to populate spinner Pin
Member 1475169122-Feb-20 14:38
Member 1475169122-Feb-20 14:38 
QuestionJson Manager Project (Need Critique/Possible Tips) Pin
Member 147385357-Feb-20 16:20
Member 147385357-Feb-20 16:20 
AnswerRe: Json Manager Project (Need Critique/Possible Tips) Pin
Richard MacCutchan7-Feb-20 22:06
mveRichard MacCutchan7-Feb-20 22:06 
GeneralRe: Json Manager Project (Need Critique/Possible Tips) Pin
Member 147385358-Feb-20 3:56
Member 147385358-Feb-20 3:56 
QuestionHow to optimize the time complexity of counting a word Pin
neelayak7-Feb-20 3:28
neelayak7-Feb-20 3:28 
AnswerRe: How to optimize the time complexity of counting a word Pin
Richard MacCutchan7-Feb-20 4:08
mveRichard MacCutchan7-Feb-20 4:08 
GeneralRe: How to optimize the time complexity of counting a word Pin
neelayak7-Feb-20 4:21
neelayak7-Feb-20 4:21 
GeneralRe: How to optimize the time complexity of counting a word Pin
Richard MacCutchan7-Feb-20 4:41
mveRichard MacCutchan7-Feb-20 4:41 
QuestionMessage Closed Pin
20-Jan-20 14:21
bekagaw65320-Jan-20 14:21 
JokeJ Pin
Member 1471420112-Jan-20 9:03
Member 1471420112-Jan-20 9:03 
SuggestionRe: Java task zigzag Pin
Richard MacCutchan12-Jan-20 22:11
mveRichard MacCutchan12-Jan-20 22:11 
AnswerRe: Java task zigzag Pin
ZurdoDev13-Jan-20 1:05
professionalZurdoDev13-Jan-20 1:05 
GeneralRe: Java task zigzag Pin
Richard MacCutchan13-Jan-20 1:33
mveRichard MacCutchan13-Jan-20 1:33 
Questionbus reservation and ticketting system Pin
Member 1469715023-Dec-19 10:20
Member 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
Member 1469715023-Dec-19 2:53 
AnswerRe: project on java Pin
Richard MacCutchan23-Dec-19 3:20
mveRichard MacCutchan23-Dec-19 3:20 
QuestionReaderWriter Problem Pin
pembepanter21-Dec-19 10:38
pembepanter21-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
pembepanter21-Dec-19 23:17 
GeneralRe: ReaderWriter Problem Pin
phil.o22-Dec-19 3:43
professionalphil.o22-Dec-19 3:43 
GeneralRe: ReaderWriter Problem Pin
pembepanter22-Dec-19 4:15
pembepanter22-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);
      }
   };
}

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.