LoginSignup
0
0

More than 3 years have passed since last update.

コンディション

Last updated at Posted at 2020-04-19
import logging
import threading
import time

logging.basicConfig(level=logging.DEBUG, format='%(threadName)s: %(message)s')


def worker1(condition):
    with condition:
        condition.wait()  #notifyAllが呼ばれるまで待機
        logging.debug('start')
        time.sleep(3)
        logging.debug('end')


def worker2(condition):
    with condition:
        condition.wait()  #notifyAllが呼ばれるまで待機
        logging.debug('start')
        time.sleep(3)
        logging.debug('end')


def worker3(condition):
    with condition:
        logging.debug('start')
        time.sleep(3)
        logging.debug('end')
        condition.notifyAll()

if __name__ == '__main__':
    condition = threading.Condition()
    t1 = threading.Thread(target=worker1, args=(condition,))
    t2 = threading.Thread(target=worker2, args=(condition,))
    t3 = threading.Thread(target=worker3, args=(condition,))
    t1.start()
    t2.start()
    t3.start()
Thread-3: start
Thread-3: end
Thread-1: start
Thread-1: end
Thread-2: start
Thread-2: end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0