在我的主代码中使用 Thread.currentThread.sleep() 有什么危险吗?

发布于 2024-07-14 12:47:37 字数 905 浏览 8 评论 0原文

在我的代码中,我

Thread.currentThread().sleep(sleepTime);

在代码的主要(非线程对象)部分中使用。

它看起来工作正常,但我担心可能会有一些隐藏的陷阱稍后会咬我的屁股。

有没有更好的方法让你的主要流程停留一段时间? 或者这是规定的方法?

编辑:

为了回答为什么我这样做......

我有一个通过 HTTP 或 FTP 连接到远程主机并执行操作的进程。

换句话说...

东西...

连接到远程...

用远程连接做东西...

关闭连接...

更多东西...

根据需要重复。

我发现在非常非常罕见的情况下,连接就会中断,变成“la la land”。 它不会失败,不会抛出任何异常,它只是消失。 而且它是阻塞的,所以没有内嵌的方法来设置计时器。

所以,我的解决方案是这样做...

东西...

启动一个带有连接的新线程...

进入一个无限循环,并在主进程中使用计时器(而不是在生成的线程中)并等待

a) ) 等待一段预设的时间,如果连接线程

b

尚未报告其已完成,则终止它并继续。

就是在主进程中,我打算休眠一段时间,醒来,看看MAX_WAIT_TIME是否已经过期。 如果没有,那就回去睡觉,再等一会儿。

它看起来(在处理器上)比坐在标准 while 循环中要高效得多,因为这确实会干扰连接线程执行其需要执行的操作。

我想我的问题确实是......这种方法有什么不安全的地方吗? 我从答案中看到,鉴于我正在做的事情,似乎没有。 也许我应该问是否有更标准化的方法?

in my code I'm using

Thread.currentThread().sleep(sleepTime);

in the main (non Thread object) portion of the code.

It appears to be working fine, but I'm worried that there might be some hidden pitfall that will bite me in the ass later.

Is there a better way to make your main process sit around for a while? or is this the prescribed methodology?

EDIT:

In answer to why I'm doing it this way...

I have a process that connects via HTTP or FTP to remote hosts and does stuff.

In other words...

stuff...

connect to remote...

do stuff with remote connection...

close connection...

more stuff...

repeat as necessary.

I've found that in very very rare instances, the connection just goes off into la la land. It doesn't fail, it doesn't throw any exception, it just goes away.
And it's blocking, so there's no in-line way to set a timer.

So, my solution is to do this...

stuff...

start new thread with connection in it...

go into an infinite loop with a timer in the MAIN process (not in the spawned thread) and wait for either

a) the connection thread to complete its task and set some flag to "done"

or

b) wait some preset amount of time and if the connection thread has not reported that it's finished, kill it and move on.

It is in the main process that I intend to sleep for some time, wake up, and see if the MAX_WAIT_TIME has expired. If not, go back to sleep and wait some more.

It seems much more efficient (on the processor) than sitting in standard while loop, since that would really interfere with the connection thread's business of doing what it needs to do.

I guess my question really is... is there anything unsafe about this approach. I see from the answers that, given what I'm doing, it looks like there isn't. Perhaps I should have asked if there is a more standardized approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

神也荒唐 2024-07-21 12:47:37

您正在编写什么样的应用程序? 这很少是一个好主意,如果您正在编写客户端 GUI,那么这是一个特别坏主意 - 当线程处于睡眠状态时,它不会响应。

如果您可以更多地说明为什么需要暂停以及您正在编写的应用程序类型,那将会有所帮助。

另一件事 - 您的调用实际上应该是:

Thread.sleep(sleepTime);

通过 currentThread() 调用它使其看起来像一个实例方法,但事实并非如此 - 它只是一个普通的静态方法。 您不能让任何其他线程休眠。

您应该查看您的 IDE 是否有一个选项可以通过对警告或错误的引用来调用静态方法 - 它会导致误导性代码(如下所示)。

What kind of application are you writing? It's rarely a good idea, and it's a particularly bad idea if you're writing a client GUI - while the thread is sleeping, it won't be responsive.

If you could give more of an indication why you need to pause and the kind of application you're writing, that would help.

One other thing - your call should really be:

Thread.sleep(sleepTime);

Calling it via currentThread() makes it look like it's an instance method, but it's not - it's just a normal static method. You can't make any other thread sleep.

You should see if your IDE has an option to make calling static methods via a reference into a warning or error - it leads to misleading code (like this).

滥情空心 2024-07-21 12:47:37

没有任何陷阱。 只要你告诉它,它就会睡多久。

您的应用程序长时间处于休眠状态的想法可能存在也可能不存在陷阱。

There's no pitfall. It will sleep for as long as you tell it to.

There may or may not be a pitfall in the idea of your application falling asleep for a prolonged period of time.

何其悲哀 2024-07-21 12:47:37

它并不危险,但在 99% 以上的情况下,当您认为需要它时,实际上并不需要。 你想做什么?

It's not dangerous, but in 99+% of cases when you think you need it, you really don't. What are you trying to do?

白首有我共你 2024-07-21 12:47:37

嗯,Thread.sleep 是一个静态方法,所以它非常具有误导性。 此外,如果您需要关闭操作,您也无法被干净地唤醒(您可以打断,但我认为这是干净的)。

Well, Thread.sleep is a static method so it is very misleading. Also you can't be woken up cleanly (you can interrupt, but I'd dispute that that is clean) in case you need the action to shutdown.

暮年 2024-07-21 12:47:37

如果您决定使用 Thread. sleep,确保您正确处理 InterruptedException

If you decide to use Thread.sleep, ensure that you handle the InterruptedException appropriately.

删除→记忆 2024-07-21 12:47:37

连接“消失”是什么意思? 当然,没有内联方式来设置计时器,但如果需要,您可以设置连接超时和读取超时。

使用无参数构造函数创建套接字,调用 connect(SocketAddress, int) 以便您可以设置超时(以毫秒为单位)。 如果此时无法建立连接,则会抛出异常。

您可以在连接之前调用 setSoTimeout() ,以便对 read() 的任何调用只会阻塞您指定的时间,而不是永远阻塞。 如果在指定的时间内无法读取数据,则会抛出异常。

What do you mean the connection just "goes away"? Sure there's no inline way to set a timer, but you can set a connect timeout AND read timeouts if you want.

Create the socket with the no-args constructor, the call connect(SocketAddress, int) so that you can set a timeout (in milliseconds). If the connection can't be established in that time, an exception is thrown.

And you can call setSoTimeout() before connecting so that any calls to read() will only block for the amount of time you specify, instead of forever. If data can't be read in the time you specified, an exception is thrown.

夕嗳→ 2024-07-21 12:47:37

AFAIR 线程.sleep() 浪费 CPU 时间,而 Object.wait(long timeout) 不会。 因此,您应该始终使用 Object.wait(long timeout) 来代替。 虽然我找不到任何支持我的论文的素材,但我认为当切换到 Object.wait(long timeout) 调用时,我们获得了很大的性能提升。

AFAIR Thread.sleep() wastes CPU time while Object.wait(long timeout) does not. So you should always use Object.wait(long timeout) instead. Although I can't find any footage that supports my thesis, I reckon that when switching to Object.wait(long timeout) calls we received a large performance gain.

颜漓半夏 2024-07-21 12:47:37

人们经常使用 Timer 来执行延迟事件,但我更喜欢 ScheduleExecutorService。 您可以对所有超时操作使用相同的线程池。 (你可以有一个大小为1的线程池)

People often use Timer to perform delayed events but I prefer the ScheduleExecutorService. You can use the same thread pool for all your timeout actions. (You can have a thread pool of size 1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文