How to achieve multithreading when one thread is asleep

I have a problem where my class executes the first run method, after which it does not go into the second limited run method.

Executing a creature program in a controller class that has a main method and a thread pool:

public class RunnableController {
    // Main method
    public static void main(String[] args) throws InterruptedException {
        try {
            RunnableController controller = new RunnableController();
            controller.initializeDb();
            controller.initialiseThreads();
            System.out.println("Polling");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initialiseThreads() {      
        try {
            threadExecutorRead = Executors.newFixedThreadPool(10);
            PollingSynchronizer read = new PollingSynchronizer(incomingQueue, dbConncetion);
            threadExecutorRead.submit(read);
        } catch (Exception e){
            e.printStackTrace();
        }   
    }
}

      

My poller class, which is fetching new data and should do a mock update:

public class PollingSynchronizer implements Runnable {
   public PollingSynchronizer(Collection<KamMessage> incomingQueue,
         Connection dbConnection) {
      super();
      this.incomingQueue = incomingQueue;
      this.dbConnection = dbConnection;
   }

   private int seqId;

   public int getSeqId() {
      return seqId;
   }

   public void setSeqId(int seqId) {
      this.seqId = seqId;
   }

   // The method which runs Polling action and record the time at which it is done
   public void run() {
      int seqId = 0;

      while (true) {
         List<KamMessage> list = null;

         try {
            list = fullPoll(seqId);

            if (!list.isEmpty()) {
               seqId = list.get(0).getSequence();
               incomingQueue.addAll(list);
               this.outgoingQueue = incomingQueue;
               System.out.println("waiting 3 seconds");
               System.out.println("new incoming message");
               Thread.sleep(3000);//at this wait I should execute run()

               //when I debug my execution stops here and throws " Class not found Exception "
               // its does not enters the message processor class 
               MessageProcessor processor = new MessageProcessor() {
                  //the run method which should fetch the message processor class.
                  final public void run() {
                     MessageProcessor(outgoingQueue).generate(outgoingQueue);
                  }
               };
              new Thread(processor).start();
            }
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      }
   }
}

      

My message processor class:

public abstract class MessageProcessor implements Runnable {
   private Connection dbConnection;
   Statement st = null;
   ResultSet rs = null;
   PreparedStatement pstmt = null;
   private Collection<KamMessage> outgoingQueue;

   public KamMsg804 MessageProcessor(Collection<KamMessage> outgoingQueue,
         Connection dbConnection) {
      this.outgoingQueue = outgoingQueue;
      this.dbConnection = dbConnection;
      return (KpiMsg804) fetchedMessages;
   }

   public Collection<KamMessage> generate(Collection<KamMessage> outgoingQueue) {
      while (true) {
         try {
            while (rs.next()) {
               KamMessage filedClass = convertRecordsetToPojo(rs);
               outgoingQueue.add(filedClass);
            }

            for (KamMessage pojoClass : outgoingQueue) {
               KamMsg804 updatedValue = createKamMsg804(pojoClass);
               System.out.print(" " + pojoClass.getSequence());
               System.out.print(" " + pojoClass.getTableName());
               System.out.print(" " + pojoClass.getAction());
               System.out.print(" " + updatedValue.getKeyInfo1());
               System.out.print(" " + updatedValue.getKeyInfo2());
               System.out.println(" " + pojoClass.getEntryTime());
            }
            return outgoingQueue;
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
   }
}

      

My problem is exactly the second run (9 method where I get an exception in the MessageProcessor class and it falls back to polling.

  • How do I implement multithreading here, since when a thread sleeps for 3 seocnds when polled, it has to update the database at the same time.
  • After which, how the data can be sent and updated back to db.

My program flow - I have three classes: 1.Controller 2.PollerSynchro 3.Msgprocessor

I have database records that are converted to POJO form and stored in a collection. With these POJOs, my classes are trying to do multiprocessing and updating in one go.

  • Controller - has a thread pool, initiates the poller class with a polling method - done

  • Poller - should poll for new incoming messages and store them in the incoming queue - done

  • MsgProcessor - should look for new incoming messages and pass them from the outgoing queue to the incoming queue - also executed

Problem:

Now my problem

  • I need to implement this update while the polling thread is sleeping for 3 seconds,

  • In my code, for the second void run () method in the Poller class, the outgoing queue is not passed and is sent to the messageprocessor class to be updated. My thread of execution only loop back to the first run method and gets the class exception.

Please help me to solve these problems.

+3


source to share


1 answer


I can't plant this, your code is a mess. However, as far as your message processor code is not running, you never start a thread created with this code:

MessageProcessor processor = new MessageProcessor() {
    // the run method which should fetch the message processor class.
    final public void run() {
         MessageProcessor(outgoingQueue).generate(outgoingQueue);                    
    }
};

      



Ignoring the vaguely named method being called, your code should look something like this:

Message processor = new MessageProcessor() {
    // the run method which should fetch the message processor class.
    final public void run() {
         MessageProcessor(outgoingQueue).generate(outgoingQueue);                    
    }
};

new Thread(processor).start();

      

+3


source







All Articles