3.1 CreatingAThread.pdf

(618 KB) Pobierz
Creating a thread
There are two ways to create a thread.

1. Using function( method )

2. Using a class.
Using function
Suppose we want to print numbers that are 65 , 66 ……90 . This numbers
are ascii code of alphabets. We use for loop for this
for i in range(65,69):
print ( i )
Now we will create a thread for this we have to import threading module
It will create a thread using the function display to run we use t.start()
First create a thread
then start a thread
t.join() it will wait for the main program
Two thread run simultaneously it will give the mixed thread ( Alphabets and
numbers )
The output will not get same always
If you want them to execute slowly. Then you can import time and use
sleep
It will slow down the program
sleep( )
With class
In this we don’t have to create objects of the thread
Zgłoś jeśli naruszono regulamin