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
t.start();
// getting the thread name by invoking the getName() method
String str = t.getName();
System.out.println(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(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
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() {
System.out.println("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
th1.start();
// getting the thread name by invoking the getName() method
String str = th1.getName();
System.out.println(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(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
// Using the constructor Thread(Runnable r)
t1.start();
}
} 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));
threadA.start();
threadB.start();
threadC.start();
}
}
class PrintLetterTask implements Runnable {
private char letter;
private int repetitions;
public PrintLetterTask(char letter, int repetitions) {
this.letter = letter;
this.repetitions = 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++) {
System.out.print(i + " ");
System.out.print(letter);
}
}
}
}
}
}
Back
class PrintNumberTask implements
Runnable {
private int count;
public PrintNumberTask(int
count) {
this.count = 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) { t.printTable(5);
for (int i = 1; i <= 5; i++) { }
System.out.println(n * i); } class MyThread2 extends Thread {
try { Table t;
Thread.sleep(400); MyThread2(Table t) {
} catch (Exception e) { this.t = t;
System.out.println(e); }
} public void run() {
} t.printTable(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);
t1.start(); Output:
t2.start(); 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) { t.printTable(5);
for (int i = 1; i <= 5; i++) { }
System.out.println(n * i); } class MyThread2 extends Thread {
try { Table t;
Thread.sleep(400); MyThread2(Table t) {
} catch (Exception e) { this.t = t;
System.out.println(e); }
} public void run() {
} t.printTable(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);
t1.start();
t2.start(); 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);
System.out.println("Count: " + count);
thread.start();
Thread.yield(); // Yield the current
// Block the main thread to wait for thread to allow the other thread to run
keyboard input
}
try {
}
System.in.read();
public void stop() {
} catch (IOException e) {
running = false;
e.printStackTrace(); Output:
}
}
} i = 1
counterTask.stop(); // 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