Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Week07/threaded_abdulsamet_kucuk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import threading

def threaded(n):
"""
A decorator that accepts an integer argument 'n' and creates,
starts, and synchronizes 'n' threads from the decorated function.
"""
def decorator(func):
def wrapper(*args, **kwargs):
threads = []

# 1. Create and start 'n' number of threads
for _ in range(n):
thread = threading.Thread(target=func, args=args, kwargs=kwargs)
thread.start()
threads.append(thread)

# 2. Synchronize the threads by waiting for them to finish
for thread in threads:
thread.join()

return wrapper
return decorator

# --- Example usage for testing ---
if __name__ == "__main__":
import time

# Decorate a sample function to run in 3 threads
@threaded(3)
def sample_task(task_name):
print(f"[{threading.current_thread().name}] Starting {task_name}...")
time.sleep(1)
print(f"[{threading.current_thread().name}] Finished {task_name}.")

print("Main program started.")
sample_task("Data Processing")
print("Main program finished. All threads successfully synchronized.")
Loading