简体   繁体   English

Java中单个类的线程减少

[英]Thread reduction for a single class in java

I have gotten my code into a state where I am creating a couple of threads and then inside those threads I use a library framework which spawns some additional threads over the life span of my application. 我已经将我的代码设置为创建两个线程的状态,然后在这些线程中使用库框架,该框架在应用程序的生命周期内产生了一些其他线程。

I have no control over how many threads are spawned inside the library framework, but I know they exist because I can see them in the eclipse debugger, I have kept the threads I use outside the library framework to a minimum, because I really don't want a multithreaded application, but sometimes you have too. 我无法控制在库框架内产生了多少个线程,但是我知道它们存在,因为我可以在eclipse调试器中看到它们,我将在库框架外使用的线程保持在最低限度,因为我确实没有不需要多线程应用程序,但有时您也有。

Now I am at the point where I need to do things with sockets and I/O, both of which are inherently hard to deal with in a multithreaded environment and while I am going to make my program thread safe i'd rather not get into the situation in the first place, or at least minimize the occurrences, the classes I am attempting to reduce multithreading in aren't time sensitive and i'd like them to complete "when they get the time". 现在,我需要处理套接字和I / O,这在本质上很难在多线程环境中处理,而当我要确保程序线程安全时,我宁愿不介入首先,或者至少是最小化这种情况,我尝试减少多线程处理的类不是时间敏感的,我希望它们“在有时间的时候”完成。 As it happens the lazy work is all in the same class definition but due to reasons, the class is instantiated a hell of a lot. 发生这种情况时,懒惰的工作全都在同一个类定义中,但是由于某些原因,该类实例化了很多。

I was wondering if it was possible to make single type classes use only one thread when instantiated from multiple threads, and how? 我想知道是否有可能使单个类型类在从多个线程实例化时仅使用一个线程,怎么办?

I imagine the only way to achieve this would be to create a separate thread specifically for handling and processing of a instances of single class type. 我想实现此目的的唯一方法是创建一个单独的线程,专门用于处理和处理单个类类型的实例。

Or do I just have to think of a new way to structure my code? 还是我只需要考虑一种新的结构化代码的方法?

EDIT: included an example of my applications architecture; 编辑:包括我的应用程序体系结构的示例;

public class Example {
    public ArrayList<ThreadTypeA> threads = new ArrayList<ThreadTypeA>();
    public static void main(String[] args) {
        threads.add(new ThreadTypeA());
        // left out how dataObj gets to ThreadTypeB for brevity 
        dataObj data = new dataObj(events);
    }
}

 public ThreadTypeA {
     public ArrayList<ThreadTypeB> newThreads = new ArrayList<ThreadTypeB>();
     public Thread thread = new Thread(this, "");
 }

 public ThreadTypeB {
     // left out how dataObj gets to ThreadTypeB for brevity 
     public libObj libObj = new Library(dataObj);
 }

 public Library {
     public Thread thread = new Thread(this, "");
     @Override
     public void editMe(dataObj) {
         dataObj.callBack();
     }
 }

 public dataObj(events) {
      public void callMe() {
          for (Event event: events) {
              event.callMe();
          }
     }
 }

there are a number of different events that can be called, ranging from writing to files making sql queries, sending emails and using proprietary ethernet-serial comms. 可以调用许多不同的事件,从写入到进行sql查询的文件,发送电子邮件以及使用专有的以太网串行通信。 I wish all events to run on the same thread, sequentially. 我希望所有事件按顺序在同一线程上运行。

Rather than having Threads, consider having Callable or Runnables. 与其拥有线程,不如考虑拥有Callable或Runnables。 These are objects which represent the work that is to be done. 这些是代表要完成的工作的对象。 Your code can pass these to a thread pool for execution - you'll get a Future. 您的代码可以将它们传递给线程池以执行-您将获得Future。 If you care about the answer, you'll call get on the future and your code will wait for the execution to complete. 如果您关心答案,则将来会调用get,并且您的代码将等待执行完成。 If it's a fire-and-forget then you can be assured it's queued and will get done in good time. 如果这是一劳永逸的事情,那么您可以放心,它正在排队,并且会在及时完成。

Generally it makes more sense to divorce your execution code from the threads that run it to allow patterns like this. 通常,将执行代码与运行它的线程分开以允许这样的模式更有意义。

To restrict thread resources use a limited thread pool: 要限制线程资源,请使用有限的线程池:

ExecutorService executor = Executors.newFixedThreadPool(4);
for (int i = 0; i < 100; ++i) {
    executor.execute(new Runnable() { ... });
}
executor.shutdown();

Also the reuse of threads of such a pool is said to be faster. 同样,据说这种池的线程重用更快。

It might be a far hope that the library does a similar thing, and maybe even has the thread pool size configurable. 该库可能会做类似的事情,甚至可以将线程池​​的大小配置为可期望的。

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

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