我正在使用一种服务来读取消息Kafka并将其推入Cassandra.
我正在使用相同的线程架构.
有消息k threads来自Kafka主题.这些写入队列,声明为:
public static BlockingQueue<>
Run Code Online (Sandbox Code Playgroud)
现在有很多线程,比如n写入Cassandra.这是执行此操作的代码:
public void run(){
LOGGER.log(Level.INFO, "Thread Created: " +Thread.currentThread().getName());
while (!Thread.currentThread().isInterrupted()) {
Thread.yield();
if (!content.isEmpty()) {
try {
JSONObject msg = content.remove();
// JSON
for(String tableName : tableList){
CassandraConnector.getSession().execute(createQuery(tableName, msg));
}
} catch (Exception e) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
content 是用于读写操作的BlockingQueue.
我在Thread线程的实现中扩展了类,并且有一定数量的线程继续执行,除非被中断.
问题是,这是使用太多的CPU.这是第一行top命令:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
46232 vishran+ 20 0 3010804 …Run Code Online (Sandbox Code Playgroud) 我在SO上找到此链接:
然后在看到答案的同时,Thread.sleep(0)在我的while循环中添加了一个内容,它可以正常工作。
问题是“为什么?”。另外,此问题是否特定于Eclipse?