python
import threading
class MyThread(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
def my_function(name):
thread1 = MyThread("Thread1")
thread2 = threading.Thread(target=my_function, args=("Thread2",))
thread1.start()
thread2.start()
python
import threading
lock = threading.Lock()
count = 0
def increment():
global count
with lock:
count += 1
threads = []
for i in range(5):
thread = threading.Thread(target=increment)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()