Copy of Multithreaded Programming
Copy of Multithreaded Programming
More on threads: pieces of code, or tasks that run within your program.
Why multithread?
Multithreading allows you to efficiently run your programs by making use of the
processing power your system has.
On the other hand, there is single-threading where you can only run one thing at a
time, so it takes an enormous amount of time and the sheer power of your
computer isn’t even being fully used.
Running your code using multithreading makes things much more efficient. Aka
less time used.
The Life of a Thread
Threads have a simple life. They exist in many states.
1. Running/Ready to run
2. Suspended - temporarily stops the thread
3. Resumed - to tell a thread to stop being suspended
4. Blocked - when waiting for a resource
5. Terminated - thread is permanently stopped
Thread Priorities
Threads have to know when to start and how to react to other threads in the
program. Each thread is assigned a number, telling it when to switch from one
thread to the next(context switch). The range of numbers is 1-10
Example: You might have one thread that loads something but while that thread is
loading something, the other thread might try and read it at the same time which
does not make sense.
Creating Threads
They can be created in 2 ways. Either by extending Thread or by implementing
Runnable. Here are some methods that come with your threads that we will be
playing around with: