Java 如何在一个线程处于睡眠模式时实现多线程

Java 如何在一个线程处于睡眠模式时实现多线程,java,multithreading,threadpool,Java,Multithreading,Threadpool,我的类在执行第一个运行方法时遇到了一个问题,之后它没有进入第二个过度运行方法 程序在控制器类中执行,该控制器类具有一个主方法和一个线程池: public class RunnableController { // Main method public static void main(String[] args) throws InterruptedException { try { RunnableController controlle

我的类在执行第一个运行方法时遇到了一个问题,之后它没有进入第二个过度运行方法

程序在控制器类中执行,该控制器类具有一个主方法和一个线程池:

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();
        }   
    }
}
我的轮询器类,它获取新数据并应模拟更新:

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();
         }
      }
   }
}
公共类轮询同步器实现可运行{
公共轮询同步器(收集输入队列,
连接(数据库连接){
超级();
this.incomingQueue=incomingQueue;
this.dbConnection=dbConnection;
}
私有int-seqId;
public int getSeqId(){
返回seqId;
}
公共无效设置seqId(内部序列ID){
this.seqId=seqId;
}
//运行轮询操作并记录完成时间的方法
公开募捐{
int-seqId=0;
while(true){
List=null;
试一试{
列表=完整投票(seqId);
如果(!list.isEmpty()){
seqId=list.get(0.getSequence();
incomingQueue.addAll(列表);
this.outgoingQueue=incomingQueue;
System.out.println(“等待3秒”);
System.out.println(“新传入消息”);
sleep(3000);//在此等待时,我应该执行run()
//当我调试时,我的执行停止在这里并抛出“类未找到异常”
//它不进入消息处理器类
MessageProcessor=新的MessageProcessor(){
//应该获取消息处理器类的run方法。
最终公开作废运行(){
MessageProcessor(outgoingQueue).generate(outgoingQueue);
}
};
新线程(处理器).start();
}
}捕获(异常e1){
e1.printStackTrace();
}
}
}
}
我的消息处理器类:

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();
         }
      }
   }
}
公共抽象类MessageProcessor实现可运行{
私有连接;
语句st=null;
结果集rs=null;
PreparedStatement pstmt=null;
私人收款支出队列;
公共KamMsg804消息处理器(收集输出队列,
连接(数据库连接){
this.outgoingQueue=outgoingQueue;
this.dbConnection=dbConnection;
返回(KpiMsg804)获取的消息;
}
公共集合生成(集合输出队列){
while(true){
试一试{
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());
}
返出队列;
}捕获(SQLE异常){
e、 printStackTrace();
}
}
}
}
我的问题正是在第二次运行(9)方法时,我在MessageProcessor类中遇到异常,它会循环回轮询

  • 我如何在这里实现多线程,因为当线程在轮询中休眠3个seocnd时,它应该同时更新数据库
  • 之后,如何将数据反馈并更新回数据库
  • 我的课程流程-我有三门课: 1.控制器 2.花粉同步器 3.MSG处理器

    我有数据库记录,这些记录被转换成POJO形式并存储在一个集合中

  • 控制器-拥有线程池,使用poll方法启动poller类-完成

  • 轮询器-应轮询新传入的消息并将其存储在传入队列中-完成

  • MsgProcessor-应该查找新的传入消息,并将它们从传出队列传递到传入队列-也完成了

  • 问题:

    现在我的问题是

  • 我必须在轮询线程休眠3秒时执行此更新

  • 在我为Poller类中的第二个void run()方法编写的代码中,传出队列没有传递并反馈给messageprocessor类进行更新。我的执行流只是循环回第一个run方法,我得到了类异常


  • 请帮助我解决这些问题。

    我无法粉饰这一点,您的代码乱七八糟。但是,就您的消息处理器代码未被执行的原因而言,您从未实际启动使用此代码创建的线程:

    MessageProcessor processor = new MessageProcessor() {
        // the run method which should fetch the message processor class.
        final public void run() {
             MessageProcessor(outgoingQueue).generate(outgoingQueue);                    
        }
    };
    
    忽略正在调用的名称混乱的方法,您的代码应该如下所示:

    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();
    

    发布准确的异常消息和stacktrace会有帮助。@matt b谢谢,我得到的异常没有明确的返回值。我想你的问题是,你的代码既糟糕又混乱,执行流也不清楚。我的建议是,扔掉你所拥有的。从试着在一张纸上可视化你的流和执行开始。然后隔离并实现每个单独的流。然后,看看如何并行化和线程化(如果必须的话)。另一件事:永远不要像构造函数那样命名方法(就像你的
    MessageProcessor
    方法)。这是一个非常混乱、非常非常糟糕的编码。退后一步。忘记类和对象-识别信息流。数据从何而来,应该如何处理,应该发送到何处。然后找出流的哪些部分相互依赖,以及数据是如何传输的