현재 3 개의 스레드가 있으며 동시에 실행 중입니다.스레드에서 교착 상태 시뮬레이션
def f1():
print "running first thread\n"
sleep(10)
def f2():
print "running second thread\n"
sleep(10)
def f3():
print "running third thread\n"
sleep(10)
if __name__ == "__main__":
thread1 = Thread(target = f1)
thread2 = Thread(target = f2)
thread3 = Thread(target = f3)
try:
thread1 = Thread(target = f1)
thread1.start()
thread2 = Thread(target = f2)
thread2.start()
thread3 = Thread(target = f3)
thread3.start()
while(thread1.isAlive() or thread2.isAlive() or thread3.isAlive()):
thread1.join()
thread2.join()
thread3.join()
except (KeyboardInterrupt, SystemExit):
sys.exit()
교착 상태를 어떻게 시뮬레이트 할 수 있습니까? 또한 각 스레드가 다른 스레드를 실행하게하려면 어떻게해야합니까? 또한 현재 스크립트에서 실행중인 모든 스레드를 나열 할 수 있습니까? 또는 우선 순위를 부여 하시겠습니까?
교착 상태를 시뮬레이트하려고합니까 (제목이 묻는 것처럼) 또는 실제로 교착 상태를 만들려고합니까? 그리고 후자의 경우 실제 상황에서 결코 발생하지 않는 교착 상태의 사소한 인공 케이스 또는 다소 현실적인 시나리오의 가장 간단한 예를 원하십니까? – abarnert
우선, 교착 상태가 발생할 수있는 잠금 장치가 필요할 수 있습니다. http://effbot.org/zone/thread-synchronization.htm – paulsm4
@abarnert; 실제로 교착 상태를 시뮬레이트해야합니다. 미안하지만 내 글을 편집하십시오. – moenad