- A Multithreaded program contains two or more parts that can run concurrently. Each part of such program is called thread.
- Multithreading enables you to write efficient programs that make maximum use of processing power available in the system.
- Multithreading introduces an asynchronous behaviour to the programs.
- When needed synchronicity can be enforced using
interprocess synchronization called the monitor.
- Monitor can be imagined as a box that can only hold single thread. once the thread enters the monitor, all other threads must wait until that thread exits the monitor.
- when any java programs starts up, one thread begins running immediately, this is the main thread of the program.
- It is created automatically, when the program starts.
- The main thread is the thread from which other "child" threads will be spawned.
- It is the last thread to finish execution since it performs various shutdown actions.
class Main{
public static void main(String[] args){
Thread t = Thread.currentThread(); // getting reference of the current thread
System.out.println(t);
t.setName("Main Thread"); // setting the name of the thread
System.out.println(t);
try {
for(int n=5;n>0;n--){
System.out.println(n);
Thread.sleep(1000);
}
}catch (InterruptedException ex){
System.out.println("Thread interrupted");
}
}
}
- Implementing
Runnable Interface
- Extending
Thread class
class CustomThread implements Runnable {
private final Thread t;
CustomThread(String name){
t = new Thread(this,name);
}
public void start(){
this.t.start();
}
/**
* Runs this operation.
*/
@Override
public void run() {
try {
for(int n=5;n>0;n--){
System.out.println("thread " + t.getName() +" >> " + n);
Thread.sleep(500);
}
}catch (InterruptedException ex){
System.out.println("thread " + t.getName() + " interrupted");
}
System.out.println("thread " + t.getName() + " exiting");
}
}
public class Main {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("main thread: " + t);
new CustomThread("t1").start(); // start the new child thread
new CustomThread("t2").start(); // start the new child thread
try {
for(int n=5;n>0;n--){
System.out.println("main thread: " + n);
Thread.sleep(1000);
}
}catch (InterruptedException ex){
System.out.println("main thread interrupted");
}
System.out.println("Main thread exiting");
}
}
class CustomThread extends Thread {
CustomThread(String name){
super(name);
}
/**
* Runs this operation.
*/
@Override
public void run() {
try {
for(int n=5;n>0;n--){
System.out.println("thread " + this.getName() +" >> " + n);
Thread.sleep(500);
}
}catch (InterruptedException ex){
System.out.println("thread " + this.getName() + " interrupted");
}
System.out.println("thread " + this.getName() + " exiting");
}
}
public class Main {
public static void main(String[] args) {
Thread t = Thread.currentThread();
System.out.println("main thread: " + t);
new CustomThread("t1").start(); // start the new child thread
new CustomThread("t2").start(); // start the new child thread
try {
for(int n=5;n>0;n--){
System.out.println("main thread: " + n);
Thread.sleep(1000);
}
}catch (InterruptedException ex){
System.out.println("main thread interrupted");
}
System.out.println("Main thread exiting");
}
}