Lab Demo on Multithreads
Main Thread
Create New Thread using
o Thread class :
• Thread constructors and extend from Thread
class
o Implementing from Runnable class
Thread Scheduling
Thread Synchronization
1 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Using the Thread Class: Thread(String
Name)
public class MyThread1 {
// Main method
public static void main(String argvs[]) {
/*creating an object of the Thread class using
the constructor Thread(String name) */
Thread t= new Thread("My first thread");
// the start() method moves the thread to the active state
[Link]();
// getting the thread name by invoking the getName() method
String str = [Link]();
[Link](str);
}
}
Output:
Back My first thread
2 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Extending Thread class Example
class Multi extends Thread{
public void run(){
[Link]("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
[Link]();
}
}
Output:
thread is running...
Back
3 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Using the Thread Class: Thread(Runnable r, String
name)
public class MyThread2 implements Runnable {
public void run() {
[Link]("Now the thread is running ...");
}
// main method
public static void main(String argvs[]) {
// creating an object of the class MyThread2
Runnable r1 = new MyThread2();
// creating an object of the class Thread using Thread(Runnable r, String na
me)
Thread th1 = new Thread(r1, "My new thread");
// the start() method moves the thread to the active state
[Link]();
// getting the thread name by invoking the getName() method
String str = [Link]();
[Link](str);
} Output:
} My new thread
Back
Now the thread is running ...
4 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Implementing Runnable interface Example
class Multi3 implements Runnable{
public void run(){
[Link]("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
// Using the constructor Thread(Runnable r)
[Link]();
}
} Output:
thread is running...
Back
5 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Program that creates three tasks
public class MultiThreadExample {
public static void main(String[] args) {
Thread threadA = new Thread(new PrintLetterTask('a', 100));
Thread threadB = new Thread(new PrintLetterTask('b', 100));
Thread threadC = new Thread(new PrintNumberTask(100));
[Link]();
[Link]();
[Link]();
}
}
class PrintLetterTask implements Runnable {
private char letter;
private int repetitions;
public PrintLetterTask(char letter, int repetitions) {
[Link] = letter;
[Link] = repetitions;
}
6 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Program that creates three tasks
public void run() {
for (int i = 0; i < public void run() {
repetitions; i++) { for (int i =1;i <=count; i++) {
[Link](i + " ");
[Link](letter);
}
}
}
}
}
}
Back
class PrintNumberTask implements
Runnable {
private int count;
public PrintNumberTask(int
count) {
[Link] = count;
}
7 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Example without Synchronization
class Table { }
//method not synchronized public void run() {
void printTable(int n) { [Link](5);
for (int i = 1; i <= 5; i++) { }
[Link](n * i); } class MyThread2 extends Thread {
try { Table t;
[Link](400); MyThread2(Table t) {
} catch (Exception e) { this.t = t;
[Link](e); }
} public void run() {
} [Link](100);
} }
} class MyThread1 extends Thread { }
Table t;
MyThread1(Table t) {
this.t = t;
8 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Example without Synchronization
public class TestSynchronization1 {
public static void main(String args[]) {
Table obj = new Table();//only one object
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
[Link](); Output:
[Link](); 5
} 100
10
} 200
15
Back 300
20
400
25
500
9 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Example with Synchronization
class Table { }
//method not synchronized public void run() {
synchronized void printTable(int n) { [Link](5);
for (int i = 1; i <= 5; i++) { }
[Link](n * i); } class MyThread2 extends Thread {
try { Table t;
[Link](400); MyThread2(Table t) {
} catch (Exception e) { this.t = t;
[Link](e); }
} public void run() {
} [Link](100);
} }
} class MyThread1 extends Thread { }
Table t;
MyThread1(Table t) {
this.t = t;
10 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Example with Synchronization
public class TestSynchronization1 {
public static void main(String args[]) {
Table obj = new Table(); //only one object
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
[Link]();
[Link](); Output:
}
2
} 10
15
Back 20
25
100
200
300
400
500
11 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
Exercise 2
public class ConcurrentExecutionExample { private int count = 0;
public static void main(String[] args) {
CounterTask counterTask = new public void run() {
CounterTask(); while (running) {
Thread thread = new count++;
Thread(counterTask);
[Link]("Count: " + count);
[Link]();
[Link](); // Yield the current
// Block the main thread to wait for thread to allow the other thread to run
keyboard input
}
try {
}
[Link]();
public void stop() {
} catch (IOException e) {
running = false;
[Link](); Output:
}
}
} i = 1
[Link](); // Stop the counter i = 2
thread i = 3
} i = 4
} class CounterTask implements Runnable { i = 5
private volatile boolean running = true; i = 6
.......
12 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024
For further reading you can use this link: Java
Multithreading
13 Ch-3: MultiThreading Compiled by: F.E. 03/01/2024