简体   繁体   English

Java线程扩展了可运行状态

[英]Java threads extended runnable state

Is there a nice way that's not CPU intensive to have a Java thread be in the Runnable state for a long period of time, like one hour? 有没有一种好方法,使Java线程长时间处于Runnable状态(例如一小时),而不会占用大量CPU?

EDIT: I'm trying to reproduce a bug. 编辑:我试图重现一个错误。 I'm suspecting a database connection is reset after a period of time, but thread sleep didn't do it. 我怀疑一段时间后会重置数据库连接,但是线程睡眠没有执行该操作。 Before I move on to other possible root causes, I want to make sure that a thread in a runnable state also doesn't cause the connection to be reset. 在继续探讨其他可能的根本原因之前,我想确保处于可运行状态的线程也不会导致连接被重置。

EDIT: I found a workaround which looks like a big fat hack. 编辑:我发现了一个变通办法,看起来像个大胖子。 Posted the answer to my own question if it helps other people at all. 将答案发布到我自己的问题上是否对其他人有帮助。

A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor. 处于可运行状态的线程正在Java虚拟机中执行,但它可能正在等待来自操作系统(例如处理器)的其他资源。

Have the processor work on something else :) 让处理器处理其他事情:)

I don't think so. 我不这么认为。 The only way a thread could stay runnable is to be CPU-bound or to be waiting for a higher-priority thread that was CPU-bound. 线程保持可运行状态的唯一方法是与CPU绑定,或者等待与CPU绑定的更高优先级的线程。

I think I kind of found a workaround just now. 我想我刚刚找到了解决方法。

If I try to connect to a non-existing domain, the thread will be in the running state until the connection times-out (I think this depends on the OS). 如果我尝试连接到不存在的域,则该线程将处于运行状态,直到连接超时(我认为这取决于操作系统)。

try {
    new URL("http://thisdomaindoesnotexist.com").
    openConnection().
    getInputStream().
    read();
} 
catch (Exception e) {}

On my machine this takes about 20 seconds. 在我的机器上,这大约需要20秒。

The System.in InputStream also works but my web application doesn't have console input. System.in InputStream也可以工作,但是我的Web应用程序没有控制台输入。

If a thread is runnable (at the OS level, there's generally a difference between "runnable" and "actually running on a CPU as we speak"), then at some point sooner or later, the OS will try to give it some CPU time. 如果线程是可运行的(在操作系统级别,“可运行的”与“实际上我们所说的实际上在CPU上运行”之间通常会有区别),那么操作系统迟早会尝试给它一些CPU时间。 There's kind of no way round that-- there's not really a category of "runnable but I don't to actually give it any CPU time". 几乎没有办法-实际上没有“可运行,但我实际上不给它任何CPU时间”的类别。 A potential option may be to give the thread a low priority: just be clear about what that actually means on your particular system (I've written a little about thread priorities and what they really mean on different OS's in case it helps). 一个可能的选择是给线程一个低优先级:只需弄清楚它在您的特定系统上的实际含义(我已经写了一些关于线程优先级的信息 ,以及它们在不同操作系统上的实际含义,以防它有帮助)。

Also have a look at Thread.yield() -- again, just be aware that it means different things on different OS's. 还要看看Thread.yield() -再次注意,它在不同操作系统上的含义不同。

However, I stress from my comment above-- I wonder if your OS is simply closing connections after a certain time open and/or at a certain scheduled time closing all idle connections, rather than things being dependent on thread state/CPU usage. 但是,我在上面的评论中强调–我想知道您的操作系统是否只是在打开某个时间之后和/或在某个预定的时间关闭所有空闲连接而关闭连接,而不是取决于线程状态/ CPU使用率。

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

相关问题 了解在Java中实现Runnable的线程 - Understanding Threads implementing Runnable in java Java线程。 可运行界面 - Java threads. Runnable interface 线程转储包含处于RUNNABLE状态且没有堆栈的线程 - Thread dump containing threads in RUNNABLE state with no stack jvm如何在可运行状态下处理许多线程 - how jvm handles many threads in runnable state WebSphere Web容器线程以可运行状态处于最大线程状态挂起 - WebSphere web container threads hungs with maximum threads state in runnable 使用Runnable或Thread出现Java线程问题 - Issue with Java threads, using Runnable or Thread ActiveMQ Transport:tcp:Thread RUNNABLE状态 - 挂起太多线程 - ActiveMQ Transport: tcp: Thread RUNNABLE state - too many threads hanging Java 线程状态转换,WAITING 到 BLOCKED,还是 RUNNABLE? - Java thread state transition, WAITING to BLOCKED, or RUNNABLE? Thread类扩展时无法同步两个线程,但在实现Runnable接口时无效 - Unable to synchronize two threads when Thread class is extended but works when implementing Runnable interface Java 6线程-具有“可运行的实现”和“扩展线程”的不同行为 - Java 6 Threads - different behaviour with “implements Runnable” and “extends Thread”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM