- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个问题,我的类正在执行第一个运行方法,之后它没有进入第二个被覆盖的运行方法。
程序执行在一个 Controller 类中,它有一个 main 方法和一个线程池:
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();
}
}
}
}
我的消息处理器类:
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();
}
}
}
}
我的问题恰好在第二次运行时(9 方法,我在 MessageProcessor 类中遇到异常并循环回轮询。
我的程序流程 - 我有三个类: 1. Controller 2.轮询同步 3.消息处理器
我有数据库记录,这些记录被转换成 POJO 形式并存储在一个 Collection 中。借助这些 POJO,我的类(class)尝试一次性完成多处理和更新。
Controller - 拥有线程池,使用 poll 方法启动 poller 类 - 完成
轮询器 - 应轮询新传入消息并将其存储在传入队列中 - 完成
MsgProcessor - 应该寻找新的传入消息并将它们从传出队列传递到传入队列 - 也已完成
问题:
现在我的问题是
我必须在轮询线程 hibernate 3 秒时实现此更新,
在我的 Poller 类中的第二个 void 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();
关于java - 如何在一个线程处于 sleep 模式时实现多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14401107/
我是一名优秀的程序员,十分优秀!