Java 处理线程池&;等待通知全部()
如何处理线程池,其中一个线程池正在轮询,而另一个线程池在处理后应该更新新的传入数据。 程序在控制器类中执行,该控制器类具有主方法和线程池: 主类控制器Java 处理线程池&;等待通知全部(),java,multithreading,concurrency,queue,Java,Multithreading,Concurrency,Queue,如何处理线程池,其中一个线程池正在轮询,而另一个线程池在处理后应该更新新的传入数据。 程序在控制器类中执行,该控制器类具有主方法和线程池: 主类控制器 public static void main(String[] args) throws InterruptedException { RunnableController controller = new RunnableController(); Accumulator acque = new Accumulator(
public static void main(String[] args) throws InterruptedException {
RunnableController controller = new RunnableController();
Accumulator acque = new Accumulator();
controller.initializeDb();
controller.initialiseThreads(acque);
controller.initialUpdate(acque);
}
轮询类的运行方法:
public void run() {
int seqId = 0;
List<KpiMessage> list = null;
while(true) {
try{
list = fullPoll(seqId);
if (!list.isEmpty()) {
accumulator.manageIngoing(list);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
public List<KpiMessage> fullPoll(int lastSeq) throws Exception {
Statement st = dbConnection.createStatement();
System.out.println("Polling");
ResultSet rs = st.executeQuery("Select * from msg_new_to_bde where ACTION = 804 and SEQ >" +
lastSeq + "order by SEQ DESC");
return pojoCol;
}
更新到数据库的方法
public void updateDb(Collection<KpiMessage> updatedQueue, Connection dbConnection) throws
SQLException{
for(KpiMessage pojoClass : updatedQueue){
Statement stmtupd = dbConnection.createStatement();
System.out.println("Updating");
String query = "UPDATE msg_new_to_bde SET KEYINFO1= 'Processed', KEYINFO2 = 'Updated'
WHERE ACTION = 804";
stmtupd.executeUpdate(query);**My Execution stops here**
public void updateDb(集合updatedQueue,连接dbConnection)抛出
SQLException{
for(KpiMessage pojoClass:updatedQueue){
语句stmtupd=dbConnection.createStatement();
System.out.println(“更新”);
String query=“UPDATE msg\u new\u to_bde SET KEYINFO1=‘已处理’,KEYINFO2=‘已更新’
其中ACTION=804”;
stmtupd.executeUpdate(查询);**我的执行在此停止**
最后是一个用于维护所有这些队列的累加器类:
public boolean isUsed = false;
public synchronized void manageIngoing(List<KpiMessage> list){
if(this.isUsed){
try {
wait();
System.out.println("first wait");
} catch (Exception e1) {
e1.printStackTrace();
}
}
System.out.println("recived pass after update");
this.getIncomingQueue().addAll(list);
//incoming queue copied to outgoing queue
this.setOutgoingQueue(this.getIncomingQueue());
System.out.println("waiting");
System.out.println("new incoming message");
this.isUsed = false;
notifyAll();
}
/**
* Method which handles synchronization using wait and notify for outgoing messages after
polling
* @param outgoingQueue
* @param dbConnection
*/
public synchronized void manageOutgoing(Collection<KpiMessage> outgoingQueue, Connection
dbConnection){
if(!this.isUsed)
{
try {
System.out.println("second wait");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.isUsed = true;
DBhandler dbhandler = new DBhandler();
try {
dbhandler.updateDb(getOutgoingQueue(), dbConnection);
} catch (SQLException e) {
e.printStackTrace();
}
notifyAll();
}
}
public boolean isUsed=false;
公共同步作废管理(列表){
如果(this.isUsed){
试一试{
等待();
System.out.println(“第一次等待”);
}捕获(异常e1){
e1.printStackTrace();
}
}
System.out.println(“更新后接收通过”);
this.getIncomingQueue().addAll(列表);
//传入队列已复制到传出队列
this.setOutgoingQueue(this.getIncomingQueue());
System.out.println(“等待”);
System.out.println(“新传入消息”);
this.isUsed=false;
notifyAll();
}
/**
*方法,该方法在发送消息后使用等待和通知来处理同步
投票
*@param outgoingQueue
*@param-dbConnection
*/
公共同步的void manager传出(集合传出队列、连接
数据库连接){
如果(!this.isUsed)
{
试一试{
System.out.println(“第二次等待”);
等待();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
this.isUsed=true;
DBhandler DBhandler=新的DBhandler();
试一试{
updateDb(getOutgoingQueue(),dbConnection);
}捕获(SQLE异常){
e、 printStackTrace();
}
notifyAll();
}
}
我的任务和问题是:
1.控制器应同时处理线程轮询器和处理器,累加器处理传入和传出队列,最后在处理后送入更新队列以更新数据库
2.我这里的类只做一次轮询,无法更新,执行在
3.此处我的wait()、notifyALL()句柄是否正确
如何在这里实现重复轮询和更新?在这个有五个不同问题的复杂环境中,很可能没有完整的答案。在等待这些问题的过程中,您应该了解java.util.concurrent提供了什么,特别是支持阻止读取的并发集合使用
wait()
和notify()
只有在JDK类对您来说还不够的情况下才能使用。在这个复杂的环境中,有五个不同的问题,很可能没有完整的答案。在等待这些问题时,您应该仔细阅读java.util.concurrent提供的功能,特别是支持阻塞读写的并发集合。Usewait()
和notify()
仅当JDK类对您来说不够时。很抱歉,您需要将其汇总或划分为不同的问题,而不是全部once@JAtin我已经按照你所说的进行了更新…@Jatin我的执行恰好在更新语句处暂停,这也不会返回到循环…很抱歉,但你需要继续或者把它分为不同的问题,而不是全部once@JAtin我已经按照你所说的进行了更新…@Jatin我的执行恰好在更新语句处暂停,它也不会返回循环…谢谢你,在我的程序中,它停止打印更新,线程等待更新…你可以找到一个很好的介绍感谢您,在我的程序中,它停止打印更新,线程等待更新……您可以在
public boolean isUsed = false;
public synchronized void manageIngoing(List<KpiMessage> list){
if(this.isUsed){
try {
wait();
System.out.println("first wait");
} catch (Exception e1) {
e1.printStackTrace();
}
}
System.out.println("recived pass after update");
this.getIncomingQueue().addAll(list);
//incoming queue copied to outgoing queue
this.setOutgoingQueue(this.getIncomingQueue());
System.out.println("waiting");
System.out.println("new incoming message");
this.isUsed = false;
notifyAll();
}
/**
* Method which handles synchronization using wait and notify for outgoing messages after
polling
* @param outgoingQueue
* @param dbConnection
*/
public synchronized void manageOutgoing(Collection<KpiMessage> outgoingQueue, Connection
dbConnection){
if(!this.isUsed)
{
try {
System.out.println("second wait");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.isUsed = true;
DBhandler dbhandler = new DBhandler();
try {
dbhandler.updateDb(getOutgoingQueue(), dbConnection);
} catch (SQLException e) {
e.printStackTrace();
}
notifyAll();
}
}