Output of Python program | Set 16 (Threads)

1) What is the output of the following program?




import threading
  
barrier = threading.Barrier(4)
  
class thread(threading.Thread):
    def __init__(self, thread_ID, thread_name):
        threading.Thread.__init__(self)
        self.thread_ID = thread_ID
        self.thread_name = thread_name
    def run(self):
        print("ThreadID = " + str(self.thread_ID) + ", ThreadName = " + 
self.thread_name + "\n")
        try:
            barrier = threading.Barrier(4)
            barrier.wait()
        except:
            print("barrier broken")
thread1 = thread(100, "GFG")
thread2 = thread(101, "Beginner")
thread3 = thread(102, "w3wiki")
  
thread1.start()
thread2.start()
thread3.start()
  
barrier.wait()
  
print("Exit")


a) ThreadID = 100, ThreadName = GFG
ThreadID = 101, ThreadName = Beginner
ThreadID = 102, ThreadName = w3wiki
b) ThreadID = 100, ThreadName = GFG
ThreadID = 101, ThreadName = Beginner
ThreadID = 102, ThreadName = w3wiki
Exit
c) Compilation error
d) Runtime error

Ans. (a)

Explanation: This is an example of deadlock. Each thread creates it’s own barrier and calls .wait() function on that barrier.

2) Which among the following is NOT the output of the following program?




import threading
  
class thread(threading.Thread):
    def __init__(self, thread_ID, thread_name):
        threading.Thread.__init__(self)
        self.thread_ID = thread_ID
        self.thread_name = thread_name
    def run(self):
        print(self.thread_name)
         
thread1 = thread(100, "GFG ")
thread2 = thread(101, "Beginner ")
thread3 = thread(102, "w3wiki ")
  
thread1.start()
thread2.start()
thread3.start()
  
print("Exit")


a) GFG Beginner w3wiki Exit
b) Exit Beginner w3wiki GFG
c) GFG Exit w3wiki Beginner
d) None of the above

Ans. (d)

Explanation: Calling start() method on a thread moves the thread to ready state. It’s the responsibility of the thread scheduler to schedule the thread. So, a particular thread can be scheduled at any instant.

3) What is the output of the following program?




import threading
  
class thread(threading.Thread):
    def __init__(self, thread_ID, thread_name):
        threading.Thread.__init__(self)
        self.thread_ID = thread_ID
        self.thread_name = thread_name
    def run(self):
        print(self.thread_name)
         
thread1 = thread(100, "GFG ")
thread2 = thread(101, "Beginner ")
thread3 = thread(102, "w3wiki ")
  
thread = []
thread.append(thread1)
thread.append(thread2)
thread.append(thread3)
  
thread1.start()
thread2.start()
for thread in thread:
    thread.join()
      
thread3.start()
  
print("Exit")


a) GFG Beginner w3wiki Exit
b) Compilation error
c) Program will halt in between due to Runtime error
d) None of these

Ans. (c)

Explanation: join() method cannot be called on a thread that hasn’t yet started it’s execution.

4) What is the output of the following program?




import threading
  
i = 5
  
class thread(threading.Thread):
    def __init__(self, thread_ID, thread_name):
        threading.Thread.__init__(self)
        self.thread_ID = thread_ID
        self.thread_name = thread_name
    def run(self):
        i = i + 1
        print(i)
          
thread1 = thread(100, "GFG ")
thread2 = thread(101, "Beginner")
  
thread1.start()
thread2.start()


a) 66
b) 67
c) Compilation error
d) Runtime error

Ans. (d)

Explanation: Each thread has it’s own space reserved in memory. So, for each threads, thread1 and thread2, the variable temp is not declared as temp is not defined within the thread’s run method.

5) What is the output of the following program?




import threading
  
class thread(threading.Thread):
    def __init__(self, thread_ID):
        self.thread_ID = thread_ID
    def run(self):
        print(self.thread_ID)
          
thread1 = thread(100)
  
thread1.start()


a) 100
b) Compilation error
c) Runtime error
d) None of these

Ans. (c)

Explanation: thread.__init__() has to be called explicitly by each of the threads being created within the __init__ function.



Contact Us