简体   繁体   English

在相同类的两个不同实例上定义的线程之间的通信?

[英]Communication between threads defined on two different instances of same class?

I have a class (say) Task.java which performs some task scheduling activity. 我有一个类(例如) Task.java ,它执行一些任务计划活动。 This class contains two types of activities- 1) To schedule periodic tasks and 2) To schedule aperiodic tasks. 此类包含两种类型的活动:1)安排定期任务和2)安排非周期性任务。 Periodic task has higher priority over aperiodic. 周期性任务比非周期性任务具有更高的优先级。

public class Task implements Runnable{
  ArrayList<> list;
  public Task(ArrayList<> list){
    this.list = list;
  }
public void run(){
    handlePeriodicTask(){...}
    handleAPeriodicTask(){...}
  }
}

In main class, I've one global list of task which contains both periodic and aperiodic tasks. 在主类中,我有一个全局任务列表,其中包含周期性任务和非周期性任务。 I am splitting this list into two sublists each containing different periodic tasks but may be repeating aperiodic tasks. 我将此列表分为两个子列表,每个子列表包含不同的定期任务,但可能会重复执行非周期性任务。 And I'm supplying these sublists to two different threads created on two different instances of Task.java 我将这些子列表提供给在Task.java两个不同实例上创建的两个不同线程

public class Main{
  ArrayList<> globalList;

  //splitting globalList to get two sublists
  ArrayList<> subList1; 
  ArrayList<> subList2;
  public static void main(..){
     Task task1 = new Task(subList1);

     Task task2 = new Task(subList1);
     new Thread(task1).start();
     new Thread(task2).start();
  }
}

I want all periodic tasks in two sublists to be scheduled independently, however aperiodic, (since they are aperiodic in nature), need prior knowledge if it is already scheduled by another thread. 我希望两个子列表中的所有定期任务都独立计划,但是非周期性的(因为它们本质上是非周期性的),如果它已经由另一个线程计划,则需要先验知识。 If so, current thread should ignore them. 如果是这样,当前线程应忽略它们。 I don't know how to achieve this communication via wait() notify() on two threads created on two different instances. 我不知道如何通过在两个不同实例上创建的两个线程上的wait() notify()实现此通信。 I've seen several examples of wait() notify() that operates on single instance, but how to achieve this on two different instances. 我已经看到了几个在单个实例上运行的wait() notify()示例,但是如何在两个不同的实例上实现这一点。 I'm not well versed in multithreading, so the question may stand silly/invalid, but I want proper guidance for that. 我不太精通多线程,所以这个问题可能很愚蠢/无效,但是我需要适当的指导。 Anybody please guide me. 有人请指导我。 Thanks. 谢谢。

Never launch thread independently, if the solution you are trying to achieve will result into generation of too many threads. 切勿单独启动线程,如果您尝试实现的解决方案将导致生成太多线程。 It is better to use Thread Pool Manager ( Executor Service - Link ). 最好使用线程池管理器( Executor Service - 链接 )。 This will help you to achieve multiple things - Launching of threads in more controlled manner, configure number of threads (poolsize) based on environment, reliable mechanism to manage threads across all platforms. 这将帮助您实现多个目标-以更受控制的方式启动线程,基于环境配置线程数量(池大小),可靠的机制来管理所有平台上的线程。 Your all periodic tasks can be given to ExecutorService to launch. 您的所有定期任务都可以交给ExecutorService启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM