【发布时间】:2017-07-05 08:52:40
【问题描述】:
我正在实现一个 Java 数据记录器,它以精确的时间间隔读取来自不同生产机器的一些数据。为了避免一个调用阻塞以下调用,我正在考虑为解析器类的每次调用创建一个新线程。
但是,这需要创建许多线程,然后每 10 秒(这是我的阅读间隔)停止它们。当解析器出现异常时(由于我正在使用的物联网设备可能超时),非并发方法会导致我有很多延迟,从而延迟下一次调用。
while(!error){
//JDBC connections and other calls here
//Queryresult is a ResultSet that returns all the machine addresses needing to be read
while(queryresult.next()){
//Parser.ParseSpeedV is the method I need to call concurrently
Double v = Parser.ParseSpeedV(..Params..);
Double s = v*queryresult.getDouble("const");
st = conn.createStatement();
st.executeUpdate("INSERT INTO ...");
}
st.close();
Thread.sleep(10000);
}
什么是实现并发方法调用(对方法 ParseSpeedV)而不产生每天启动的数千个线程造成的开销的最佳方法?
【问题讨论】:
标签: java multithreading logging concurrency