Opening Hours :7AM to 9PM
Without Using Threading Rules:
Program: Display 1-10 number with 2sec time gap #!/usr/bin/python import threading import time def myfun(): counter=1 while counter<=10: time.sleep(2) print(counter) counter =counter+ 1 if __name__=="__main__": myfun()
class Mythread(threading.Thread): def __init__(self): threading.Thread.__init__(self) Def run(self): …. …. …. if __name__=="__main__": m=Mythread() m.start()
Write a Python program to display message a 2sec time gap.
#!/usr/bin/python import threading import time class myThread (threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): counter=1 while counter<=10: time.sleep(2) print(counter,self.name) counter =counter+ 1 if __name__=="__main__": thread1 = myThread() # Start new Threads thread1.start()
#!/usr/bin/python import threading import time class thDemo(threading.Thread): def run(self): x=10 y=20 z=x+y print(z) time.sleep(2) z=x-y print(z) if __name__=="__main__": t=thDemo() t.start()
run() − The run() method is the entry point for a thread.
start() − The start() method starts a thread by calling the run method.
join([time]) − The join() waits for threads to terminate.
isAlive() − The isAlive() method checks whether a thread is still executing.
getName() − The getName() method returns the name of a thread.
setName() − The setName() method sets the name of a thread.
The threading module provided with Python includes a simple-to-implement locking mechanism that allows you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock. The acquire(blocking) method of the new lock object is used to force threads to run synchronously. The optional blocking parameter enables you to control whether the thread waits to acquire the lock. If blocking is set to 0, the thread returns immediately with a 0 value if the lock cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1, the thread blocks and wait for the lock to be released. The release() method of the new lock object is used to release the lock when it is no longer required.
Without synchronized
#!/usr/bin/python import threading import time a=1 class myThread (threading.Thread): def __init__(self,name): threading.Thread.__init__(self) def run(self): w=1; global a print("Available are ", a) if(a>=w): print(w,"reserverd ",self.name) a=a-w; time.sleep(2) else: print("sorry!....no tickets avalavle"); # Create new threads if __name__=="__main__": thread1 = myThread("t1") thread2 = myThread("t2") thread3 = myThread("t3") # Start new Threads thread1.start() thread2.start() thread3.start()
#!/usr/bin/python import threading import time a=1 class myThread (threading.Thread): def __init__(self,name): threading.Thread.__init__(self) def run(self): w=1; global a threadLock.acquire() print("Available are ", a) if(a>=w): a=a-w; print(w,"reserverd ",self.name) time.sleep(2) else: print("sorry!....no tickets avalavle"); threadLock.release() threadLock = threading.Lock() #threads = [] if __name__=="__main__": # Create new threads thread1 = myThread("t1") thread2 = myThread("t2") thread3 = myThread("t3") # Start new Threads thread1.start() thread2.start() thread3.start()