Chapter 5 Multithreading Concepts
Chapter 5 Multithreading Concepts
Bedasa Wayessa
(a) Here multiple threads are running on multiple CPUs. (b) Here multiple
threads share a single CPU.
Java Programming CoSc3053 8
Multithreading
More than one thread (program) run simultaneously is known as
multithreading (multiprogramming).
In multithreading java interpreter handles the switching of control
between the threads in such a way that it looks like they are running
concurrently.
Multithreading is useful in a number of ways. We can divide a long
program into number of threads and executes them in parallel.
We are going to learn so many useful things in thread lets start with
single main thread.
thread.start();
run:
This is from Main Thread : main
Output This is from secondary Thread : Thread-0
Java Programming CoSc3053 17
Creating Thread by Extending the Thread
package MultiThreading;
firstThread.start();
secondThread.start();
while (secondThread.isAlive() && firstThread.isAlive()) {
System.out.println("First Thread is " + firstThread.getState());
System.out.println("second Thread is " + secondThread.getState());
}
firstThread.stop(); First Thread is RUNNABLE
secondThread.stop(); Hello Tesfanesh
}
Hello Duressa
} second Thread is RUNNABLE
This approach is, however, not recommended because it mixes the task and the mechanism
of running the task. Separating the task from the thread is a preferred design.
thread.start();
System.out.println("This is from Main Thread : " +
Thread.currentThread().getName());
}
run:
This is from Main Thread : main
Output This is from secondary Thread : Thread-0
Java Programming CoSc3053 22
Creating Thread by Implementing Runnable
package MultiThreading; run:
public class NamesRunnable implements Runnable {
Hello Duressa
First Thread is RUNNABLE
public static void main(String args[]) { Hello Tesfanesh
NamesThread names = new NamesThread(); second Thread is RUNNABLE
Thread firstThread = new Thread(names, "Duressa");
Thread secondThread = new Thread(names, "Tesfanesh");
firstThread.start();
secondThread.start();
while (secondThread.isAlive() && firstThread.isAlive()) {
System.out.println("First Thread is " + firstThread.getState());
System.out.println("second Thread is " + secondThread.getState());
}
firstThread.stop();
secondThread.stop();
}
void displayBalance() {
System.out.println("Account No: " + accountNo +
" Balance: " + balance);
}
//synchronized void deposit(int amount) {
void deposit(int amount) {
balance = balance + amount;
System.out.println(amount + " is deposited");
displayBalance();
}
void withdraw(int amount) {
balance = balance - amount;
System.out.println(amount + " is withdrawn");
displayBalance();
}
}
Java Programming CoSc3053 37
Thread Synchronization Example
package MultiThreading;
}
Output
500 is deposited
Account No: 1010 Balance: 500 is updated by 3
You cant withraw this 500 amount
Account No: 1010 Balance: 500 ss updated by 2
Java Programming CoSc3053 40
Thread Synchronization
Concepts you have to understanding in Thread Synchronization
– Resource sharing - data , file, disk etc ….
– Critical section - part of code that access required resource
– Mutual exclusion – way of preventing simultaneous access
– Locking or mutex – lock and start using while the other waiting
– Semaphore – uses wait() and signal()
– Monitor
– Race condition
– Inter-Thread communications
https://docs.oracle.com/en/java/