Multi Threading Notes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

References

C. Horstmann, Object-Oriented Design & Patterns


Lecture 3.1 2nd ed., John Wiley & Sons, 2005

Multithreading

Thread Basics Thread Examples


Threads enable multiple tasks to run In a car racing game several cars run
simultaneously simultaneously. You will need separate
How could multiple task run concurrently in a thread for each car.
single CPU? Web browser can load multiple images into a
The operating system frequently switches web page at the same time.
back and forth between threads (tasks), giving
illusion that they run in parallel

3 4
Java Threads Creating thread in Java
A thread is a program unit that is executed 1. Define a class which you want to run as a
independently of other parts of the program. separate thread and implement the
Runnable interface. This interface has a
Java virtual machine executes each thread single method called run.
for a short amount of time and then switches
public interface Runnable{
to another thread. You can visualize the
void run();
thread as programs executing in parallel to
}
each other.
2. Place the code for the task into the run
method of the class.
5 6

Cont. Java Example


class greet implements Runnable{
3. Create an object of the class private String greeting;
public greet(String greeting){
4. Construct a Thread object and supply the this.greeting = greeting;
}
above object in the constructor. public void run(){
try{
5. Call the start method of the Thread object to for (int i = 1; i <= 5; i++){
System.out.println(i +": " + greeting);
start the thread. Thread.sleep(500);
}
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}

7 8
Cont. Terminating Thread
public class mythread { A thread terminates when the run method returns.
public static void main(String [] args){ This is normal way of terminating a thread.
Runnable r1 = new greet("Hello");
Runnable r2 = new greet("bye");
If there is need to abruptly terminate a thread during
Thread t1 = new Thread(r1); execution, a thread must be interrupted.
Thread t2 = new Thread(r2); To notify a thread that it should clean up and terminate,
use the interrupt method
threadobject.interrupt();
t1.start();
t2.start();
This call does not terminate the thread; it merely sets a
}
flag in the thread data structure.
}

9 10

Cont. Interrupting thread


In the initial release of Java library, the To interrupt a thread type:
Thread class had a stop method to terminate threadobject.interrupt();
a thread. However, that method is now
deprecated because stopping a thread can
lead to problematic situation if a thread holds To check whether the current thread has
a lock on shared resources and it is not been interrupted write the following code:
released. if (Thread.currentThread().isInterrupted())…

11 12
Race Conditions
A race condition occurs if the effect of
multiple threads on shared data depends on
the order in which the threads are scheduled.
All threads will compete to get hold of shared
resources.

13

You might also like