当前位置: 首页 > 面试题库 >

我可以暂停和继续的线程?

廉雅惠
2023-03-14
问题内容

我正在尝试创建一个线程,该线程在后台执行操作。我需要能够在需要时有效地“暂停”并稍后再次“恢复”。另外,如果我“暂停”该线程时正在执行某项操作,则该线程应使调用线程等到完成其操作为止。

我对Python中的多线程技术还很陌生,所以我还没走那么远。

除了线程正在做某事时调用了pause之外,我几乎可以做的所有事情都让调用线程等待。

这是我要在代码中实现的目标的概述:

import threading, time

class Me(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        #flag to pause thread
        self.paused = False

    def run(self):
        while True:
            if not self.paused:
                #thread should do the thing if
                #not paused
                print 'do the thing'
                time.sleep(5)

    def pause(self):
        self.paused = True
        #this is should make the calling thread wait if pause() is
        #called while the thread is 'doing the thing', until it is
        #finished 'doing the thing'

    #should just resume the thread
    def resume(self):
        self.paused = False

我想我基本上需要一种锁定机制,但是在同一线程内?


问题答案:

Condition可以用于。

这是填写您的骨骼的示例:

class Me(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        #flag to pause thread
        self.paused = False
        # Explicitly using Lock over RLock since the use of self.paused
        # break reentrancy anyway, and I believe using Lock could allow
        # one thread to pause the worker, while another resumes; haven't
        # checked if Condition imposes additional limitations that would 
        # prevent that. In Python 2, use of Lock instead of RLock also
        # boosts performance.
        self.pause_cond = threading.Condition(threading.Lock())

    def run(self):
        while True:
            with self.pause_cond:
                while self.paused:
                    self.pause_cond.wait()

                #thread should do the thing if
                #not paused
                print 'do the thing'
            time.sleep(5)

    def pause(self):
        self.paused = True
        # If in sleep, we acquire immediately, otherwise we wait for thread
        # to release condition. In race, worker will still see self.paused
        # and begin waiting until it's set back to False
        self.pause_cond.acquire()

    #should just resume the thread
    def resume(self):
        self.paused = False
        # Notify so thread will wake after lock released
        self.pause_cond.notify()
        # Now release the lock
        self.pause_cond.release()

希望能有所帮助。



 类似资料:
  • 本文向大家介绍JavaScript暂停和继续定时器的实现方法,包括了JavaScript暂停和继续定时器的实现方法的使用技巧和注意事项,需要的朋友参考一下 对于JavaScript的定时器来说没有严格意义上的暂停和重启,只有清除停止,但是可以通过一些‘障眼法'实现 以上所述是小编给大家介绍的JavaScript暂停和继续定时器的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复

  • 就异步编程而言,这可能是一个非常基本的问题,但我试图阅读它,但找不到任何相同的资源。 假设:我对异步编程有一个大致的想法: 当我们开始阻塞操作(网络调用,从DB/文件中读取)时,我们可以将其委托给内核线程,这将使我们的应用程序线程保持空闲,以执行其他任务。内核线程等待作业完成,并在作业完成时向应用程序线程提供回调。 科鲁蒂斯:过去几天我一直在读关于科特林·科鲁蒂斯的书。不过,我认为概念wise c

  • 我正在 Canvas 中创建一个示例游戏到表面视图中。 我正在使用更新和绘图方法,所有工作。现在我试图暂停并恢复游戏。我在网上找到的方法奏效了,但是当游戏恢复时,它要慢得多。 我的循环游戏: 我的暂停和恢复方法是: 有人知道为什么慢下来吗?以及解决方案?(注:我试图给出一个固定的睡眠而不是变量,结果是同样的慢) 谢谢。 @编辑 我发现了问题。 我的活动在Resume()上有一个监听器,这会启动线程

  • 问题内容: 我正在尝试在来电中暂停录音并在以后恢复录音。我正在使用andriod 并尝试在中录制。我尝试通过重设/停止录音来暂停/恢复,并以开头,是已停止/暂停的音频文件的,希望它可以追加,但是我没有运气。有没有办法实现这一目标或追加两张录音,或者我应该放弃。 码: 停止录制,但恢复失败并显示消息。 问题答案: 您问题的简单答案是: 您不能吗? 因此,在Stop停止后,尝试保存对SDCard的呼叫

  • 本文向大家介绍IOS 中动画的暂停与继续播放的详解,包括了IOS 中动画的暂停与继续播放的详解的使用技巧和注意事项,需要的朋友参考一下 IOS 中动画的暂停与继续播放的详解 在使用动画控制UI的时候,可能会碰到通过手势或其他方式要进行暂停正在进行中的动画,然后再继续。如手指按下时,暂停动画,手指离开时继续动画。 实现原理主要是通过UI的layer进行相关的控制。 暂停动画: 继续动画: 使用示例:

  • 我使用Java的rabbitmq-client(https://mvnrepository.com/artifact/com.rabbitmq/amqp-client),我需要实现以下场景: 在接收Rabbit消息时,如果怀疑内存中不适合所有等待的数据,则可能需要暂停特定队列的Rabbitmq消耗。 处理完一些消息后,需要再次打开以下一组消息的消耗。 根据需要重复。 使用amqp-client J