PROGRAMS ON THREADS
1. Program to create a thread using Thread class
/*Program to illustrate creation of threads using Thread class*/
class MyThread extends Thread{
public void run(){
for(int i=0; i<5; i++){
[Link]("Thread is running......!");
try{
[Link](1000);
catch(InterruptedException e){
[Link]();
public static void main(String args[]){
MyThread t=new MyThread();
[Link]();
for(int i=0; i<5; i++){
[Link]("main() is running......!");
try{
[Link](1000);
catch(InterruptedException e){
[Link]();
1|P a g e
OUTPUT:
2. Program to create a thread using Runnable interface
/*Program to illustrate creation of threads using Runnable interface*/
class MyThread1 implements Runnable{
public void run(){
for(int i=0; i<5; i++){
[Link]("Thread is running......!");
try{
[Link](1000);
catch(InterruptedException e){
[Link]();
public static void main(String args[]){
MyThread1 mt=new MyThread1();
Thread t=new Thread(mt);
[Link]();
for(int i=0; i<5; i++){
2|P a g e
[Link]("main() is running......!");
try{
[Link](1000);
catch(InterruptedException e){
[Link]();
OUTPUT:
3. Program to create multiple threads using Thread class
/*Program that creates 3 threads by extending Thread class. First thread
displays “Good Morning” every 1 second, the second thread displays “Hello”
every 2 seconds and the third thread displays “Welcome” every 3 seconds.*/
class MyThread1 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("Good Morning!!");
try{
[Link](1000);
}
3|P a g e
catch(InterruptedException e){
[Link]();
class MyThread2 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("Hello!!");
try{
[Link](2000);
}catch(InterruptedException e){
[Link]();
class MyThread3 extends Thread{
public void run(){
for(int i=0;i<5;i++){
[Link]("Welcome!!");
try{
[Link](3000);
}catch(InterruptedException e){
[Link]();
}
4|P a g e
class ThreadDemo{
public static void main(String args[]){
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
MyThread3 t3=new MyThread3();
[Link]();
[Link]();
[Link]();
OUTPUT:
4. Program to create multiple threads using Runnable interface
/*Program that creates 3 threads by implementing Runnable interface. First
thread displays “Good Morning” every 1 second, the second thread displays
“Hello” every 2 seconds and the third thread displays “Welcome” every 3
seconds.*/
class MyThread1 implements Runnable{
public void run(){
for(int i=0;i<5;i++){
[Link]("Good Morning!!");
try{
5|P a g e
[Link](1000);
}
catch(InterruptedException e){
[Link]();
}
}
}
}
class MyThread2 implements Runnable{
public void run(){
for(int i=0;i<5;i++){
[Link]("Hello!!");
try{
[Link](2000);
}
catch(InterruptedException e){
[Link]();
}
}
}
}
class MyThread3 implements Runnable{
public void run(){
for(int i=0;i<5;i++){
[Link]("Welcome!!");
try{
[Link](3000);
}
catch(InterruptedException e){
[Link]();
}
}
}
}
class ThreadDemo1{
public static void main(String args[]){
MyThread1 rt1=new MyThread1();
6|P a g e
Thread t1=new Thread(rt1);
MyThread2 rt2=new MyThread2();
Thread t2=new Thread(rt2);
MyThread3 rt3=new MyThread3();
Thread t3=new Thread(rt3);
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
5. Program to illustrate isAlive() & join() methods of Thread class
/*Program to illustrate isAlive() and join() methods of Thread class*/
class MyThread extends Thread{
public void run(){
[Link]("From "+[Link]()+": Hai!");
try{
[Link](1000);
}
catch(InterruptedException e){
[Link]();
}
[Link]("From "+[Link]()+": Hello!");
7|P a g e
}
}
class ThreadMethodsDemo{
public static void main(String arg[]){
MyThread t1=new MyThread();
[Link]("Is t1 alive? : "+[Link]());
MyThread t2=new MyThread();
[Link]("Is t2 alive? : "+[Link]());
[Link]();
[Link]("Is t1 alive? : "+[Link]());
try{
[Link]();
}
catch(InterruptedException e){
[Link]();
}
[Link]();
[Link]("Is t2 alive? : "+[Link]());
}
}
OUTPUT:
8|P a g e
5. Program to illustrate Daemon threads
/*Program to illustrate Daemon threads*/
class MyThread extends Thread{
public void run(){
if([Link]().isDaemon()){
[Link]("Daemon Thread executing...");
}
else{
[Link]("Normal Thread executing.....");
}
try{
[Link](1000);
}
catch(InterruptedException e){
[Link]();
}
}
}
class DaemonThreadDemo{
public static void main(String arg[]){
MyThread t1=new MyThread();
MyThread t2=new MyThread();
[Link]("Is t1 a Deamon? : "+[Link]());
[Link]("Is t2 a Deamon? : "+[Link]());
[Link](true);
[Link]("Is t1 a Deamon? : "+[Link]());
[Link]("Is t2 a Deamon? : "+[Link]());
[Link]();
try{
[Link]();
}
catch(InterruptedException e){
[Link]();
}
9|P a g e
[Link]();
}
}
OUTPUT:
5. Program to illustrate thread synchronization to solve producer-consumer problem
/*program to solve Producer-Consumer problem using multithreading in Java*/
import [Link];
import [Link];
/*This class has a list, producer (adds items to list) and consumer (removes items
from list)*/
class PC{
// Create a list shared by producer and consumer
LinkedList<Integer> list = new LinkedList<>();
int capacity;
//constructor
PC(int n){
capacity=n;
// Function called by producer thread
public void produce() throws InterruptedException{
int value = 1;
while (true){
10 | P a g e
synchronized (this){
// producer thread waits while list is full
while ([Link]()==capacity)
wait();
[Link]("Producer produced item-" + value);
// to insert the jobs in the list
[Link](value);
value++;
// notifies consumer thread that it can start consuming now
notify();
// and then sleep
[Link](2000);
// Function called by consumer thread
public void consume() throws InterruptedException{
while (true){
synchronized (this){
// consumer thread waits while list is empty
while ([Link]()==0)
wait();
//to retrive the ifrst job in the list
int val = [Link]();
[Link]("Consumer consumed item-" + val);
// Wake up producer thread
notify();
// and sleep
[Link](2000);
11 | P a g e
}
public class ProducerConsumer{
public static void main(String[] args) throws InterruptedException{
Scanner s=new Scanner([Link]);
//Reading the size of the buffer
[Link]("Enter the size of the buffer: ");
int c=[Link]();
// Object of a class that has both produce() and consume() methods
PC pc = new PC(c);
// Create producer thread
Thread t1 = new Thread(new Runnable(){
public void run(){
try{
[Link]();
catch(InterruptedException e){
[Link]();
});
// Create consumer thread
Thread t2 = new Thread(new Runnable(){
public void run(){
try{
[Link]();
catch(InterruptedException e){
[Link]();
}
12 | P a g e
}
});
// Start both threads
[Link]();
[Link]();
// t1 finishes before t2
[Link]();
[Link]();
OUTPUT:
13 | P a g e