You are on page 1of 2

Emily Mendoza

PDP Quiz
11/17/2022

FIRST CODE
The code in the threading and time modules are being imported to be accessible in the current
code we are using.The start of the class “MyThread” with the threading.Thread in the parenthesis
will be what is indexed at the end of the code.The __init__ function has self, name, and delay as
parameters which will call when using the class. Same as I said before, the
threading.Thread.__init__(self) line will make a thread run when the name of the class is called
with the self.name = name and self.delay parameters being called. Using the self before the
variables will allow the variable to be used in other functions of the class as well. In the next
function “run” it is saying as the thread is running print out “Starting thread name”, count down
the name and delay of the thread, and print out “Finished thread name”. In this function,
thread_count_down has the parameters of the class name and delay. Function sets a bound of 5
and has the threads count down from 5 using a while loop. This will print the name of the thread
and increment them down. These next two lines are defining thread 1 and 2 and are putting the
variables through the class using the parameters and having the names and a 0.5 sec delay.
Initiating the separate threads and Joins the threads to have them run in parallel. Prints when all
the tasks are actually finished.

SECOND CODE
Importing the square root from the math module, importing _thread and using it as thread in the
code, and importing time. Function is_prime has a parameter of (x). The code is saying if x is
less than 2 print is not a prime number. Else if x is equal to 2 print is a prime number. Else if x is
modulus of 2 and that equals zero than x is not a prime number. Otherwise, set a variable limit
equal to the square root of x plus one in a loop where x in the given range is equal to zero to not
equal a prime number. In the same breath if that modulus is not zero it is a prime number. Next a
set of numbers is made to use in the code. For each number in the set start a new thread and run
is_prime function.

THIRD CODE
First thread will be importing the threading module. This seems to be an addition to the code
right above. The class “MyThread” has a parameter that will be initiating the thread when called.
The __init__ function has the parameters of self and x which are variables that are usable
throughout the class and x will be an input for the class. The run function will start processing as
the class is called. When it runs it will perform the operations to see if the input numbers are
prime numbers. My_input holds the values to be tested and an empty array ‘threads’ is created.
The following loop states that for x values in my_input, make a thread, start running it, and
append that to the threads array. The loop right after that is ensuring that the threads run in
parrellel. Last, when everything is done “finished” will be printed.

FOURTH CODE
This code is importing the modules threading and time. Class myThread will have an input
parameter that starts the thread when called. The _init_ function defines the input parameters of
name and delay. The run function is Starting the thread, locking the said thread and running the
countdown of that thread, and lastly unlocking it so the next thread can run right after. The
function thread_count_down will give both name and display a countdown of 5. The contents
state that while the counter is going down increment from 5 down to 0. While each number is in
play delay for the given time before counting down to the next number. Thread_lock is
referencing the Lock() function outside of the class so it is defined before the run of the class.
Threads 1 and 2 are run with a 0.5 second delay between each count. A runs first then be runs
second.

FIFTH CODE
Importing the modules queue, threading, and time. Class myThread has a parameter that will run
a thread when called. The init function will define the parameter name that will be input for the
class and also be used throughout the class. The run function will start a thread and have other
threads sit in a queue until the first is finished, then exit the current thread to move onto the
queue. Outside of the class is a process_queue function. This function states that while the
function is initiated and true, have x be the thread queued except if the cue is empty. Otherwise
print only the factors that serve as x. The next function print_factors says that using the
parameter of x, the resulting string will print what the positive factors of x are. For the number in
the range of 1 to x if the modulus of the number in the range is equal to 0 than the result string
will be printed. The cariables are then set up and put into the queue. Then Threads A, B, and C
are created using the class. They are started, ran, then done is printed when finished.

You might also like