gpt4 book ai didi

java - 我怎样才能在这个任务中只运行一次任务

转载 作者:行者123 更新时间:2023-12-01 04:33:14 25 4
gpt4 key购买 nike

我的 WebApplication 中有下面这段代码,每当用户登录到应用程序时,它基本上都会执行此操作。

  1. addSymbols 方法将在用户登录操作期间被调用。
  2. 由于上述原因,PriorityBlockingQueue 被启动
  3. PriorityBlockingQueue 将符号添加到 HashSet 中,然后迭代该符号以在新添加的符号上运行特定任务。

我在这里面临的问题是,作为 HashSet 一部分的 while 循环正在一遍又一遍地执行相同的任务,因为它处于线程的 while true 条件内。

我粘贴示例输出是为了了解与其相关的上下文。

Symbol From priorityBlocking  SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest
Symbol From allSymbolsSet SymbolTest

这是我的代码,它执行上述操作。

package com;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;

public class TaskerThread extends Thread {
private PriorityBlockingQueue<String> priorityBlocking = new PriorityBlockingQueue<String>();
private Set<String> allSymbolsSet = new HashSet<String>();
ExecutorService es = Executors.newFixedThreadPool(2);

public void addSymbols(String str) {
if (str != null) {
priorityBlocking.add(str);
}
}

public void run() {
while (true) {
try {
while (priorityBlocking.peek() != null) {
String symbol = priorityBlocking.poll();
allSymbolsSet.add(symbol);
try {
System.out.println("Symbol From priorityBlocking" +" "+ symbol);
} catch (Exception e) {
e.printStackTrace();
}
}
Iterator<String> ite = allSymbolsSet.iterator();
while (ite.hasNext()) {
String symbol = ite.next();
if (symbol != null && symbol.trim().length() > 0) {
try {
System.out.println("Symbol From allSymbolsSet"+" "+symbol);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
try {
TaskerThread qT = new TaskerThread();
qT.start();
qT.addSymbols("SymbolTest");
} catch (Exception e) {
e.printStackTrace();
}
}
}

请告诉我是否有任何方法可以使迭代器任务仅运行一次。

最佳答案

我明白了。这是发生的事情:

循环开始,并且符号被添加到队列中。它在线程循环中从队列中取出,然后运行迭代器循环。然后外层循环 (while (true)) 再次循环,这一次,没有符号可添加,但迭代器循环仍然执行。仅当添加了新符号时,您才需要执行迭代器循环:

            boolean added = false;
while (priorityBlocking.peek() != null) {
added = true;
String symbol = priorityBlocking.poll();
allSymbolsSet.add(symbol);
try {
System.out.println("Symbol From priorityBlocking" +" "+ symbol);
} catch (Exception e) {
e.printStackTrace();
}
}
if (added) {
Iterator<String> ite = allSymbolsSet.iterator();
while (ite.hasNext()) {
String symbol = ite.next();
if (symbol != null && symbol.trim().length() > 0) {
try {
System.out.println("Symbol From allSymbolsSet"+" "+symbol);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

或者,使用 takepoll(long,TimeUnit)当您没有任何可添加的内容时,也会停止循环。这也应该防止循环的第二部分不必要地运行。您只需要处理 InterruptedException

关于java - 我怎样才能在这个任务中只运行一次任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17696053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com