Teile 10 bis 12.
This commit is contained in:
37
teil12/threadpools.py
Normal file
37
teil12/threadpools.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import concurrent.futures
|
||||
import random
|
||||
import time
|
||||
|
||||
gesamtwert = 0
|
||||
message = ''
|
||||
|
||||
|
||||
def calculate_sum(thread_nummer):
|
||||
global gesamtwert
|
||||
global message
|
||||
|
||||
for i in range(100 + thread_nummer):
|
||||
zwischen = gesamtwert
|
||||
#Zufällige Wartezeit zwischen 0,1s und 0,5s
|
||||
wartezeit = random.randint(1,5+thread_nummer)
|
||||
time.sleep(0.1 * wartezeit)
|
||||
|
||||
zwischen += 1
|
||||
|
||||
gesamtwert = zwischen
|
||||
|
||||
message = f'Thread {thread_nummer} fertig'
|
||||
print(f'Thread + {thread_nummer} abgearbeitet.')
|
||||
|
||||
def main():
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
|
||||
threads = [executor.submit(calculate_sum, i+1) for i in range(3)]
|
||||
|
||||
results = [t.result() for t in threads]
|
||||
|
||||
print(f'gesamtwert={gesamtwert}')
|
||||
print(f'message={message}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user