简体   繁体   English

如果程序不等待用户输入/系统资源,java中线程的用途是什么

[英]what is the use of thread in java if a program is not waiting for user input/ system resources

I have a general question: 我有一个普遍的问题:

my program will just go on processing something which does not require user input or system resources (like printer etc..) meaning, my program will not wait for any resources except CPU time. 我的程序将继续处理不需要用户输入或系统资源(例如打印机等)的东西,这意味着我的程序将不等待除CPU时间以外的任何资源。

The same program (let us say job) may be initiated by multiple users. 同一程序(让我们说作业)可以由多个用户启动。

in this case, is it worth full to run this in a thread (meaning each user will get a feeling that his job is executed without delay. or is it better to run the jobs sequentially? 在这种情况下,是否值得在线程中运行它(这意味着每个用户都会感觉到自己的作业已被立即执行而没有延迟。还是最好依次执行作业?

The issue with running as separate threads is that, too many threads running simultaneously forcing the CPU utilization go over 100%. 作为单独的线程运行的问题是,同时运行的太多线程迫使CPU利用率超过100%。

Please suggest. 请提出建议。 Assume that user donot see his job progress. 假设用户看不到他的工作进度。 User is not worried when his job is finished. 用户在完成工作时不会担心。 But at the same time, I want to have the CPU busy running the jobs. 但是同时,我想让CPU忙于运行作业。

If you don't care how long a process takes, or the length of time it takes is acceptable, then using one thread is likely to be the simplest solution. 如果您不关心进程花费多长时间或花费多少时间,那么使用一个线程可能是最简单的解决方案。 For example, many GUI applications only use one event handling thread. 例如,许多GUI应用程序仅使用一个事件处理线程。

If you want to keep all your CPUs busy you can start a number of busy loops to max out all the CPUs. 如果要使所有CPU保持繁忙,则可以启动多个繁忙循环以最大程度地利用所有CPU。

What you usually want is to reduce latency, or improve threadput by using more CPUs. 通常,您希望通过使用更多的CPU来减少延迟或提高线程吞吐量。 Unless this is a goal, using more CPUs won't help you. 除非这是一个目标,否则使用更多的CPU将无济于事。

If the thread is genuinely purely CPU-bound, then it doesn't make sense to create more threads than there are cores (or virtual cores) available to process them. 如果线程确实是纯粹受 CPU约束的,那么创建更多线程(没有可用的内核(或虚拟内核))就没有意义。 So on a quad-core machine, create no more than four threads (and probably only three, as your process isn't the only thing going on on the machine). 因此,在四核计算机上,最多创建四个线程(并且可能只有三个线程,因为您的进程不是计算机上唯一发生的事情)。 On a quad-core machine with hyper-threading (two virtual threads per core), you might create six or seven. 在具有超线程的四核计算机上(每个内核两个虚拟线程),您可以创建六个或七个。 Creating too many additional threads (say, hundreds) causes unnecessary context-switching, which can be expensive if you really overdo it. 创建过多的额外线程(例如数百个)会导致不必要的上下文切换,如果您确实过度使用它,则可能会非常昂贵。

The converse is that on a multi-core machine, a single thread can only run on one core. 相反,在多核计算机上, 单个线程只能在一个核上运行。 So on a quad-core machine, running the jobs sequentially on a single thread will only utilize 25% of the CPU capacity. 因此,在四核计算机上,在单个线程上顺序运行作业将仅占用25%的CPU容量。

So: Run the jobs in parallel up to the number of available cores, and sequentially (on each core) beyond that. 因此:并行运行作业,直到可用核心数为止,然后依次(在每个核心上)运行。

Big caveat: Your mileage may vary. 请注意:您的里程可能会有所不同。 There are lots of inputs to this equation, including what else is going on on the machine, and particularly whether the jobs really are CPU-bound (as opposed to system-bound, eg, CPU and I/O subsystem and such). 这个方程式有很多输入,包括机器上正在发生的其他事情,尤其是作业是否真的受CPU约束(与系统约束相对,例如CPU和I / O子系统等)。

I guess your program needs memory access. 我猜你的程序需要内存访问。 Memory access may be slow, and you really want to run the processor at that time. 内存访问速度可能很慢,并且您当时确实想运行处理器。 A common solution to limit the number of threads running at the same time is to use a thread pool . 限制同时运行的线程数的常见解决方案是使用线程池

in this case, is it worth full to run this in a thread (meaning each user will get a feeling that his job is executed without delay. or is it better to run the jobs sequentially? 在这种情况下,是否值得在线程中运行它(这意味着每个用户都会感觉到自己的作业已被立即执行而没有延迟。还是最好依次执行作业?

It depends highly on the job. 这在很大程度上取决于工作。 If it is interactive then running it immediately would give a better interface to the user. 如果它是交互式的,则立即运行它将为用户提供更好的界面。 If the speed of the response is not an issue then maybe you don't want to incur the complexity costs of writing a multi-threaded program. 如果响应速度不是问题,那么您可能不想承担编写多线程程序的复杂性。

The issue with running as separate threads is that, too many threads running simultaneously forcing the CPU utilization go over 100%. 作为单独的线程运行的问题是,同时运行的太多线程迫使CPU利用率超过100%。

I wouldn't worry about this. 我不会为此担心。 One of the reasons why we use multiple threads is that we can make use of multiple processors to actually get the job done faster. 我们使用多个线程的原因之一是,我们可以使用多个处理器来真正更快地完成工作。 In this case, depending on the OS, you can actually see more than 100% load for the process if you are using more than a full CPU -- this is expected. 在这种情况下,如果使用的CPU超过一个完整的CPU,则取决于操作系统,实际上可以看到该进程的负载超过100%-这是预期的。 Also, if the CPU goes over 100%, the operating system will handle it fine unless you are worried that your application will be taking cycles away from a more important application. 另外,如果CPU超过100%,操作系统将对其进行处理,除非您担心您的应用程序将花费一个更重要的应用程序周期。

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

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