唯一标识 JAVA 中终止的线程
Uniquely Identify the terminated thread in JAVA
假设我有一个名为 USERS 的列表,其中包含 USER1 和 USER2。
一次最大线程数可以是USERS的大小。在这种情况下 2.
并且创建的线程将与用户同名
我有另一个包含一些值的列表。在这种情况下,假设我们有
List<String> values = new ArrayList<>();
values.add("A");values.add("B");values.add("C");values.add("D");
USER1 和 USER2 必须从 "values" 中选取数据。
所以我们启动线程,线程的最大数量可以是USERS的大小,即2。
我们启动两个线程并为它们分配 "values" 列表中的前两个值。
现在 USER1--有--> A 和 USER2--有--> B.
现在,USER2 线程结束,USER1 仍在 运行。
USER2 已完成。我再次需要创建一个新线程来处理来自 "values".
的剩余数据
那么我怎么知道 USER2 线程已经完成并创建一个名为 USER2 的新线程。
how am I supposed to know that USER2 thread has finished
1。您需要您的线程向某人报告它的终止。有人会,
interface ThreadListener {
void onThreadTerminated(String threadName);
}
2。现在创建你的线程,例如当它终止时它向 ThreadListener
.
报告它的状态
class MyThread extends Thread {
private ThreadListener listener;
MyThread(ThreadListener listener) {
this.listener = listener;
}
public void run() {
System.out.println(getName() + " starting execution.");
doWork();
listener.onThreadTerminated(getName());
}
public void doWork(){
try {
Thread.sleep(1500);
} catch (Exception e) {}
}
}
3。最后你需要线程状态的利益相关者的具体实现,
class Listener implements ThreadListener {
public void onThreadTerminated(String threadName) {
System.out.println(threadName + " finished execution");
}
}
4。在您的程序中使用它并应用您自己的逻辑,
public static void main(String[] args) {
Listener l = new Listener();
MyThread t = new MyThread(l);
t.setName("MyThread");
t.start();
}
输出,
MyThread开始执行。
MyThread 完成执行(1.5 秒后)
首先,您使用 BlockingQueue
而不是列表。
BlockingQueue<String> queue = new ArrayBlockingQueue<>(list.size(), true, list);
然后,您为每个消费者(即您的情况下的用户)启动一个线程,从队列中取出项目,直到它为空。
ExecutorService executor = Executors.newThreadPoolExecutor(users.size());
for (final User user : users) {
executor.submit(() -> {
try {
while (!queue.isEmpty()) {
String item = queue.poll(1, TimeUnit.SECONDS);
if (item != null) {
user.handle(item);
}
}
} catch (InterruptedException x) {}
});
}
现在您将提交与用户数量相等的线程数,每个线程将继续从队列中取出项目并将其交给适当的用户,直到 none 留在队列中.
假设我有一个名为 USERS 的列表,其中包含 USER1 和 USER2。 一次最大线程数可以是USERS的大小。在这种情况下 2. 并且创建的线程将与用户同名
我有另一个包含一些值的列表。在这种情况下,假设我们有
List<String> values = new ArrayList<>();
values.add("A");values.add("B");values.add("C");values.add("D");
USER1 和 USER2 必须从 "values" 中选取数据。
所以我们启动线程,线程的最大数量可以是USERS的大小,即2。
我们启动两个线程并为它们分配 "values" 列表中的前两个值。
现在 USER1--有--> A 和 USER2--有--> B.
现在,USER2 线程结束,USER1 仍在 运行。
USER2 已完成。我再次需要创建一个新线程来处理来自 "values".
的剩余数据那么我怎么知道 USER2 线程已经完成并创建一个名为 USER2 的新线程。
how am I supposed to know that USER2 thread has finished
1。您需要您的线程向某人报告它的终止。有人会,
interface ThreadListener {
void onThreadTerminated(String threadName);
}
2。现在创建你的线程,例如当它终止时它向 ThreadListener
.
class MyThread extends Thread {
private ThreadListener listener;
MyThread(ThreadListener listener) {
this.listener = listener;
}
public void run() {
System.out.println(getName() + " starting execution.");
doWork();
listener.onThreadTerminated(getName());
}
public void doWork(){
try {
Thread.sleep(1500);
} catch (Exception e) {}
}
}
3。最后你需要线程状态的利益相关者的具体实现,
class Listener implements ThreadListener {
public void onThreadTerminated(String threadName) {
System.out.println(threadName + " finished execution");
}
}
4。在您的程序中使用它并应用您自己的逻辑,
public static void main(String[] args) {
Listener l = new Listener();
MyThread t = new MyThread(l);
t.setName("MyThread");
t.start();
}
输出,
MyThread开始执行。
MyThread 完成执行(1.5 秒后)
首先,您使用 BlockingQueue
而不是列表。
BlockingQueue<String> queue = new ArrayBlockingQueue<>(list.size(), true, list);
然后,您为每个消费者(即您的情况下的用户)启动一个线程,从队列中取出项目,直到它为空。
ExecutorService executor = Executors.newThreadPoolExecutor(users.size());
for (final User user : users) {
executor.submit(() -> {
try {
while (!queue.isEmpty()) {
String item = queue.poll(1, TimeUnit.SECONDS);
if (item != null) {
user.handle(item);
}
}
} catch (InterruptedException x) {}
});
}
现在您将提交与用户数量相等的线程数,每个线程将继续从队列中取出项目并将其交给适当的用户,直到 none 留在队列中.