Java中可以控制每个线程执行的时间吗?
我想控制每个线程使用的时间量。
一个线程进行一些处理,另一个线程处理数据库中的数据,但由于生成的数据量较大,插入速度比处理速度慢。我想给处理器更多时间来插入该数据。
可以用线程来做到这一点吗?目前,我正在执行处理的线程中休眠,但是插入时间根据机器而变化。我还有其他方法可以做到这一点吗?这种方式是否涉及在我的程序中使用线程同步?
I want to control the amount of time that each thread uses.
One thread does some processing and another processes data in the database, but the insertion is slower than processing because of the amount of generated data. I want to give more processor time to insert that data.
Is it possible do this with threads? At the moment, I'm putting a sleep in the thread doing the processing, but the time of insertion changes according to the machine. Is there another way I can do this? Is the way involving the use of thread synchronization inside my program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Thread.setPriority(...) 但这并不理想。
也许您可以使用某种形式的 阻塞队列来自 java.util.concurrent 包,使一个线程在另一个线程正在做某事时等待。例如, SynchronousQueue可用于从一个线程向另一个线程发送一条消息,表明它现在可以做某事。
另一种方法是使用 Runnables 而不是 Threads,并将 Runnables 提交给 Executor,例如 ThreadPoolExecutor。该执行器的作用是确保 Runnables 使用相当多的时间。
You can increase the priority of a thread using Thread.setPriority(...) but this is not ideal.
Perhaps you can use some form of blocking queue from the java.util.concurrent package to make one Thread wait while another Thread is doing something. For example, a SynchronousQueue can be used to send a message from one Thread to another Thread that it can now do something.
Another approach is to use Runnables instead of Threads, and submit the Runnables to an Executor, such as ThreadPoolExecutor. This executor will have the role of making sure Runnables are using a fair amount of time.
首先要提到的是,线程优先级本身并不意味着“CPU 份额”。关于线程优先级的实际含义似乎存在很多混乱,部分原因是它在不同操作系统下实际上意味着不同的东西。如果您在 Linux 中工作,它实际上确实意味着接近 CPU 的相对份额。但在Windows下,肯定不行。因此,如果它有任何帮助,您可能首先需要查看我不久前编译的一些关于 Java 中的线程优先级,它解释了线程优先级在不同系统上的实际含义。
您的问题的一般答案是,如果您希望线程占用特定份额的 CPU,最好以编程方式隐式执行此操作:定期,对于处理的每个“块”,测量经过了多少时间(或使用了多少 CPU)使用 - 严格来说它们不是同一件事),然后睡眠适当的时间,以便处理/睡眠比率大致达到您想要的处理时间的百分比。
但是,我不确定这实际上对您的任务有帮助。
据我了解,基本上你有一个插入任务,这是速率确定步骤。在一般情况下,系统不太可能“故意为运行该插入的线程分配比其能力或需要更少的 CPU”。
因此,查看该插入任务并查看是否可以通过编程方式更改该插入任务的功能可能会有更多的里程。例如:您可以大批量插入吗?如果由于某种原因插入过程确实受 CPU 限制(我对此表示怀疑),您可以使用多线程吗?为什么您的应用程序实际上关心等待插入完成,您可以更改该依赖性吗?
如果插入到标准数据库系统,我想知道该插入是否会严重受 CPU 限制?
The first thing to mention is that thread priority doesn't per se mean "share of the CPU". There seems to be a lot of confusion about what thread priority actually means, partly because it actually means different things under different OS's. If you're working in Linux, it actually does mean something close to relative share of CPU. But under Windows, it definitely doesn't. So in case it's of any help, you may firstly want to look at some information I compiled a little while ago about thread priorities in Java, which explains what Thread Priorities Actually Mean on different systems.
The general answer to your question is that if you want a thread to take a particular share of CPU, it's better to implicitly do that programmatically: periodically, for each "chunk" of processing, measure how much time elapsed (or how much CPU was used-- they're not strictly speaking the same thing), then sleep an appropriate amount of time so that the processing/sleep ratio comes to roughly the % of processing time you intended.
However, I'm not sure that will actually help your task here.
As I understand, basically you have an insertion task which is the rate determining step. Under average circumstances, it's unlikely that the system is "deliberately dedicating less CPU than it can or needs to" to the thread running that insertion.
So there's probably more mileage in looking at that insertion task and seeing if programmatically you can change how that insertion task functions. For example: can you insert in larger batches? if the insertion process really is CPU bound for some reason (which I am suspicious of), can you multi-thread it? why does your application actually care about waiting for the insertion to finish, and can you change that dependency?
If the insertion is to a standard DB system, I wonder if that insertion is terribly CPU bound anyway?
一种方法是将处理线程的优先级设置为低于另一个。但请注意,不建议这样做,因为它不会使您的代码平台保持独立。 (不同的线程优先级在不同的平台上表现不同)。
另一种方法是使用一个服务,其中数据库线程将不断发送有关其当前状态的消息(可能是某个标志“aboutToOver”)。
或者使用同步(例如二进制信号量)。当数据库线程工作时,另一个线程将被阻塞,因此数据库线程将使用所有资源。但同时处理线程又会被阻塞。实际上,这将是最好的解决方案,因为 processign 线程可以执行 3-4 个任务,然后会被信号量阻塞,直到稍后它可以再次启动并执行任务
One way would be to set the priority of the processing thread to be lower than the other. But beware this is not recommended as it wont keep your code platform independent. (DIfferent thread priorities behave differently on different platforms).
Another way would be to use a service where database thread would keep sending messages about its current status (probably some flag "aboutToOver").
Or use synchronization say a binary semaphore. When the database thread is working, the other thread would be blocked and hence db thread would be using all the resources. But again processing thread would be blocked in the mean time. Actually this will be the best solution as the processign thread can perform say 3-4 tasks and then will get blocked by semaphore till later when it can again get up and do task