You are on page 1of 24

Threads and concurrent

programming
Multithreading and its application
Multithreading programs in OOP
1
Thread synchronization

multithread interpreted by : Asaminew G. 2/1/2018


2 Multithreading
Threads : -
separate computation process
computations that execute in parallel
Single sequential flow of execution
Some parts of program
an object of the class Thread
can share memory & files
can communicate with each other
multiple in one process
can be created, executed and destroyed

multithread interpreted by : Asaminew G. 2/1/2018


3 Cont. …
Thread state
New – New thread
Running – Active (ready) Thread
Waiting – wait thread for another thread to perform a task
Timed wait – sleep for specified time interval
Blocked – Suspended
Terminated – Dead (killed) – finish its task/error occur

multithread interpreted by : Asaminew G. 2/1/2018


4 Cont. …

multithread interpreted by : Asaminew G. 2/1/2018


5 Cont. …
Multithread program:
Multiple thread, which is running simultaneously
Take place when more than one threads are created and started
Application of multithreading programs
Socket programming
Programs which is solve unresponsiveness programs
Example of multithreaded application
Web Servers to handle multiple requests.
Development IDE like Visual Studio to provide intelligence while typing.
Text editors to check errors.
Web Browsers to create multiple tabs

multithread interpreted by : Asaminew G. 2/1/2018


6 Creating multithreading programs in OOP
The normal way to program a thread is to define a class that is
a derived class of the class Thread. An object of this derived
class will be a thread that follows the programming given in the
definition of the derived (Thread) class’s method named run().
Any thread class inherits the method start from the class Thread
An invocation of start() by an object of a thread class will start
the thread and invoke the method run() for that thread.
Note : - a thread (method) that create new thread is called main
thread while new created thread is called child thread and it is
independent of the main thread.

multithread interpreted by : Asaminew G. 2/1/2018


7 Cont. …
Thread class methods :
run () method – contain a code for a thread and does nothing by default
- must be override in derived class of thread class, to do
what you want the thread to do
Start () method - initiates the computation (process) of the calling thread
- It performs some overhead associated with starting a
thread and then it invokes the run () method for the thread.
Sleep () is a static method in the class Thread that pauses the thread that
includes the invocation. It pauses for the number of milliseconds (thousandths of
a second) given as an argument. It is invoked as Thread.sleep(), because
static method is accessed using class name.

multithread interpreted by : Asaminew G. 2/1/2018


Cont. …
8
The method Thread.sleep may throw an InterruptedException, which is a checked
exception and so must be either caught in a catch block or declared in a throws clause.
The classes Thread and InterruptedException are both in the package java.lang, so
neither requires any import statement.
Note that Thread.sleep can be invoked in an ordinary (single thread) program
EXAMPLE
try
{ Thread.sleep(1000); //Pause for a second }
catch (InterruptedException e) { System.out.println("Unexpected interrupt"); }
In the following program, after you press Draw button, if you press close button the application
never end until it complete drawing circle i.e. program is unresponsive to another action until it
completes the first task. This problem is solved using multithreads.

multithread interpreted by : Asaminew G. 2/1/2018


Example: program without thread

multithread interpreted by : Asaminew G. 2/1/2018


Example: program without thread

10

multithread interpreted by : Asaminew G. 2/1/2018


Output

11


Before
drawing

multithread interpreted by : Asaminew G. 2/1/2018


Output

12

While drawing: If you click


the close-window button
while the circles are being
drawn,
the window will not close until
all
the circles are drawn

multithread interpreted by : Asaminew G. 2/1/2018


Output

13


After
drawing

multithread interpreted by : Asaminew G. 2/1/2018


Fixing a Nonresponsive Program Using Threads
14
Here is why the close-window button is nonresponsive: The method fill , which draws the
circles, is invoked in the body of the method btn_drawActionPerformed. So, the method
btn_drawActionPerformed does not end until the method fill ends. And, until the method
btn_drawActionPerformed ends, the GUI cannot go on to do the next thing, which is
probably to respond to the close-window button.
Solution : using multithreading program - We have the method btn_drawActionPerformed
create a new (independent) thread to draw the circles. Once btn_drawActionPerformed
this, the new thread is an independent process that proceeds on its own. The method
btn_drawActionPerformed has nothing more to do with this new thread; the work of
btn_drawActionPerformed is ended. So, the main thread (the one with
btn_drawActionPerformed) is ready to move on to the next thing, which will probably be to
respond promptly to a click of the close-window button. At the same time, the new thread
draws the circles. So, the circles are drawn, but at the same time a click of the close-
window button will end the program

multithread interpreted by : Asaminew G. 2/1/2018


Example using thread
15

multithread interpreted by : Asaminew G. 2/1/2018


Cont. …
16

multithread interpreted by : Asaminew G. 2/1/2018


Cont. …
17

multithread interpreted by : Asaminew G. 2/1/2018


Creating thread using runnable interface
18
Instead of driving class from Thread class, we can implement Runnable interface which has only run ( ) method with
syntax of : public void run();
A class that implements the Runnable interface must still be run from an instance of the classThread. This is usually
done by passing the Runnable object as an argument to the thread constructor. The following is an outline of one
way to do this:
public class ClassToRun extends SomeClass implements Runnable
{
public void run ( ) {
//Fill this just as you would if ClassToRun
//were derived from Thread.
}
public void startThread()
{
Thread theThread = new Thread( this );
theThread.start();
}
}

multithread interpreted by : Asaminew G. 2/1/2018


Example using Runnable interface
19

multithread interpreted by : Asaminew G. 2/1/2018


Cont. …
20

multithread interpreted by : Asaminew G. 2/1/2018


Cont. …
21

multithread interpreted by : Asaminew G. 2/1/2018


22 Thread synchronization
If there are more than one thread run at the same time and they have common
resource such fields, memory address, and etc., we synchronize them to dispatches
resources through threads. While one thread is using shared resources, other
threads must wait it until it completes its job.
We can wait by invoking the join ( ) method for every thread we create. This
method waits for the thread to complete. The join() method throws
InterruptedException. This is a checked exception so we must use the try/catch
mechanism. We use synchronized keyword to lock some region. Syntax :
try {
Thread t = new Thread(Runnable r);
t.start ( );
t.join ( ); // main threads is waited until this thread is finish its job
}

multithread interpreted by : Asaminew G. 2/1/2018


Cont. …
23

multithread interpreted by : Asaminew G. 2/1/2018


24

THANK YOU

multithread interpreted by : Asaminew G. 2/1/2018

You might also like