From 02287a1bd34080fe45fd3898ed3e389defe13a2b Mon Sep 17 00:00:00 2001 From: 61XeNoN <148587793+61XeNoN@users.noreply.github.com> Date: Wed, 3 Jun 2026 18:40:56 +0300 Subject: [PATCH] Create threaded_abdulsamet_kucuk.py --- Week07/threaded_abdulsamet_kucuk.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Week07/threaded_abdulsamet_kucuk.py diff --git a/Week07/threaded_abdulsamet_kucuk.py b/Week07/threaded_abdulsamet_kucuk.py new file mode 100644 index 00000000..f65c580b --- /dev/null +++ b/Week07/threaded_abdulsamet_kucuk.py @@ -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.")