简体   繁体   English

Java中的简单多线程

[英]Simple Multi-Threading in Java

Currently, I'm running on a thread-less model that isn't working simply because I'm running out of memory before I can process the data I'm being handed. 目前,我正在运行的无线程模型无法运行,这仅仅是因为我在处理要处理的数据之前内存不足。 I've made all the changes that I can to optimize the code, and it's still just not quite quick enough. 我已经进行了所有可以优化代码的更改,但仍然不够快。

Clearly I should move on to a threaded model. 显然,我应该继续讲线程模型。 I'm wondering what the simplest, easiest way to do the following is: 我想知道执行以下操作的最简单,最简单的方法是:

  • The main thread passes some info to the worker 主线程将一些信息传递给工作人员
  • That worker performs some work that I'll refactor out of the main method 那位工人完成了一些我会从主要方法中重构的工作
  • The workers will disappear and new ones will be instantiated when needed 工人将消失,需要时将实例化新工人

I've never worked with java threading and from what I've read up on it seems pretty complicated, even if what I'm looking for seems pretty simple. 我从来没有使用过Java线程,从我读过的内容来看,它看起来很复杂,即使我正在寻找的东西看起来很简单。

If you have multiple independent units of work of equal priority, the best solution is generally some sort of work queue, where a limited number of threads (the number chosen to optimize performance) sit in a while(true) loop dequeuing work units from the queue and executing them. 如果您有多个具有相同优先级的独立工作单元,则最佳的解决方案通常是某种工作队列,其中有限数量的线程(为优化性能while(true)选择的数量)位于while(true)循环中,以从排队并执行它们。

Generally the optimum number of threads is going to be the number of processors +/- 1, though in some cases a larger number will be optimal if the threads tend to get stalled by disk I/O requests or some such. 通常,最佳线程数将是+/- 1的处理器数,但是在某些情况下,如果线程趋于因磁盘I / O请求或某些此类请求而停滞,则更大的线程数将是最佳的。

But keep in mind that tuning the entire system may be required. 但是请记住,可能需要调整整个系统。 Eg, you may need more disk arms, and certainly more RAM may be required. 例如,您可能需要更多的磁盘臂,当然可能需要更多的RAM。

I'd start by having a read through Java Concurrency as refresher ;) 我将从阅读Java并发作为复习开始;)

In particular, I would spend some time getting to know the Executors API as it will do most of what you've described without a lot of the overhead of dealing with to many locks ;) 特别是,我将花一些时间来了解Executors API,因为它可以完成您所描述的大部分操作,而不会处理很多锁;

Distributing the memory consumption to multiple threads will not change overall memory consumption. 将内存消耗分配给多个线程不会改变整体内存消耗。 From what I read out of your question, I would like to step forward and tell you: Increase the heap of the Java engine, this will help. 从我从您的问题中读出的信息,我想向前走,并告诉您:增加Java引擎的堆,这将有所帮助。 Looks like you have to optimize the Java startup parameters and not your code. 看起来您必须优化Java启动参数,而不是代码。 If I am wrong, then you will have to buffer the data. 如果我错了,那么您将不得不缓冲数据。 To Disk! 到磁盘! Not to a thread in the same memory model. 不在同一内存模型中的线程。

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

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