0% found this document useful (0 votes)
2 views

Thread

The document provides an overview of multithreading in programming, explaining the concept of threads as lightweight processes that share the same data space. It details the use of the threading module, including methods for creating and managing threads, as well as synchronization techniques using locks. Additionally, it introduces the Queue module for managing a multithreaded priority queue with various control methods.

Uploaded by

shaikhmohd9870
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Thread

The document provides an overview of multithreading in programming, explaining the concept of threads as lightweight processes that share the same data space. It details the use of the threading module, including methods for creating and managing threads, as well as synchronization techniques using locks. Additionally, it introduces the Queue module for managing a multithreaded priority queue with various control methods.

Uploaded by

shaikhmohd9870
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Thread:
▪ Threads sometimes called light-weight processes and they do not require much memory
overhead; they are cheaper than processes.
▪ Multiple threads within a process share the same data space with the main thread.
▪ Starting a New Thread
▪ you need to call following method available in thread module:
▪ thread.start_new_thread ( function, args)
▪ calls function with the passed list of args.
▪ args is a tuple of arguments.
▪ Multithreaded Programming:
▪ Thread Module:
▪ The threading module exposes all the methods of the thread module and provides some
additional methods:
• threading.activeCount(): Returns the number of thread objects that are active.
• threading.currentThread(): Returns the number of thread objects in the caller's thread control.
• threading.enumerate(): Returns a list of all the thread objects that are currently active.
▪ In addition to the methods, the threading module has the Thread class that implements
threading.
▪ The methods provided by the Thread class are as follows:
• 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.

(2) Creating a thread:


▪ To implement a new thread using the threading module, you have to do the following− ▪
Define a new subclass of the Thread class.
▪ Override the init (self [,args]) method to add additional arguments.
▪ Then, override the run(self [,args]) method to implement what the thread should do when
started.
▪ you can create an instance of it and then start a new thread by invoking the start(), ▪ which
in turn calls the run()method.

(3) Synchronizing 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 the 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.

(4)multithreaded priority queue:


▪ The Queue module allows you to create a new queue object that can hold a specific number of
items.
There are following methods to control the Queue −
• get(): The get() removes and returns an item from the queue.
• put(): The put adds item to a queue.
• qsize() : The qsize() returns the number of items that are currently in the queue.
• empty(): The empty( ) returns True if queue is empty; otherwise, False.
• full(): the full() returns True if queue is full; otherwise, False.

You might also like