Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from threading import Thread, activeCount, Event
- from random import randrange
- def resetList(size):
- list = []
- for i in range(size):
- list.append(None)
- return list
- def myFunction(threadID, end):
- print "Start__%d (End - %d)" %(threadID, end)
- Event().wait(timeout=end)
- print "Finish_%d (End - %d)" %(threadID, end)
- threadCount = 5
- threads = resetList(threadCount)
- j = 0
- for i in range(randrange(50)):
- threads[j] = Thread(target=myFunction, args=(j+1, randrange(threadCount*2)))
- threads[j].start()
- if activeCount() > threadCount:
- while activeCount() > threadCount:
- pass
- for t in range(threadCount):
- if not threads[t].isAlive():
- print 'Thread_%d Can Be Replaced' %(t+1)
- j = t
- break
- else:
- j += 1
- #Make Sure No Index Out Of Range
- if j == threadCount:
- j=0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement