Experiment No: 11
1. To implement Multithreading
a. Write a multithreaded program a java program to print Table of Five, Seven and Thirteen
using Multithreading (Use Thread class for the implementation).
Code:
class TableOf5 extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
try
{
System.out.println("5 * "+i+"= "+(i*5));
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
class TableOf7 extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
try
{
System.out.println("7 * "+i+"= "+(i*7));
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
class TableOf13 extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
try
{
System.out.println("13 * "+i+"= "+(i*13));
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
class PrintTables
{
public static void main(String args[])
{
TableOf5 five=new TableOf5();
TableOf7 seven =new TableOf7();
TableOf13 thirteen=new TableOf13();
five.start();
seven.start();
thirteen.start();
}
/*
OUTPUT:
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>javac PrintTables.java
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>java PrintTables
7 * 1= 7
13 * 1= 13
5 * 1= 5
5 * 2= 10
13 * 2= 26
7 * 2= 14
7 * 3= 21
13 * 3= 39
5 * 3= 15
7 * 4= 28
13 * 4= 52
5 * 4= 20
7 * 5= 35
5 * 5= 25
13 * 5= 65
7 * 6= 42
13 * 6= 78
5 * 6= 30
7 * 7= 49
13 * 7= 91
5 * 7= 35
7 * 8= 56
13 * 8= 104
5 * 8= 40
7 * 9= 63
13 * 9= 117
5 * 9= 45
7 * 10= 70
13 * 10= 130
5 * 10= 50
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>
*/
b. Write a multithreaded program to display /*/*/*/*/*/*/*/* using 2 child threads.
Code:
class MultiThreading implements Runnable
{
public void run()
{
if(Thread.currentThread().getName().equals("star"))
{
for(int i=1;i<=10;i++)
{
try
{
System.out.print("*");
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
if(Thread.currentThread().getName().equals("slash"))
{
for(int i=1;i<=10;i++)
{
try
{
System.out.print("/");
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
class PrintSymbolsByThread
{
public static void main(String args[])
{
Thread t1,t2;
t1=new Thread(new MultiThreading());
t2=new Thread(new MultiThreading());
t1.setName("slash");
t2.setName("star");
t1.start();
t2.start();
}
}
/*
OUTPUT:
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>javac
PrintSymbolsByThread.java
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>java
PrintSymbolsByThread
/*/*/*/*/*/*/*/*/*/* /*
C. Write a multithreaded program that generates the Fibonacci sequence. This program should
work as follows: create a class Input that reads the number of Fibonacci numbers that the
program is to generate. The class will then create a separate thread that will generate the
Fibonacci numbers, placing the sequence in an array. When the thread finishes execution, the
parent thread (Input class) will output the sequence generated by the child thread. Because
the parent thread cannot begin outputting the Fibonacci sequence until the child thread
finishes, the parent thread will have to wait for the child thread to finish.
Code:
import java.util.*;
public class Input
{
public static void main(String[] args)
{
int no;
Scanner sc=new Scanner(System.in);
System.out.println("Enter No of elements to be generate in Fibonacci series: ");
no=sc.nextInt();
FibonacciThread f = new FibonacciThread(no);
f.start();
synchronized(f) //Lock an object
{
try
{
System.out.println("Parent Thread Waiting for Fibonacci Thread to generate
all fibonacci Numbers and store the same in an array...");
f.wait();
}catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Fibonacci Thread has been Notified to Parent Class: Series
has been generated Successfully.");
System.out.println("Main Thread(parent Thread) Printing the Fibonacci Elements
from Array...");
f.printFibonacci(no);
System.out.println("Parent Thread(Main Thread) has completed its Porcess....");
}
}
}
class FibonacciThread extends Thread
{
int noOfElements,fib1=0,fib2=1,fib3,fib[];
FibonacciThread(int no)
{
noOfElements=no;
}
@Override
public void run()
{
synchronized(this)
{
fib=new int[noOfElements];
fib[0]=fib1;
fib[1]=fib2;
System.out.println("Fibonacci Thread processing to generate Series of "+noOfElements+"
Elements...");
for(int i=2; i<noOfElements ; i++)
{
try
{
fib3=fib1+fib2;
fib[i]=fib3;
fib1=fib2;
fib2=fib3;
System.out.println("...");
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
notify(); //Notify to Parent thread that Process is completed take over the control to process
further task
}
}
public void printFibonacci(int n)
{
System.out.println("Fibonacci Series: ");
for(int i=0; i<n ; i++)
{
System.out.print(fib[i]+"\t");
}
System.out.println();
}
}
/*
OUTPUT:
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>java Input
Enter No of elements to be generate in Fibonacci series:
10
Parent Thread Waiting for Fibonacci Thread to generate all fibonacci Numbers and store the same in
an array...
Fibonacci Thread processing to generate Series of 10 Elements...
...
...
...
...
...
...
...
...
Fibonacci Thread has been Notified to Parent Class: Series has been generated Successfully.
Main Thread(parent Thread) Printing the Fibonacci Elements from Array...
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
Parent Thread(Main Thread) has completed its Porcess....
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>
*/
d. WAP to prevent concurrent booking of a ticket using the concept of thread synchronization.
Code:
// Example that shows multiple threads
// can execute the same method but in
// synchronized way.
import java.util.*;
class TicketBooking
{
// if multiple threads(trains) trying to access
// this synchronized method on the same Object
// but only one thread will be able
// to execute it at a time.
Scanner sc=new Scanner(System.in);
synchronized public void bookTicket()
{
int fare;
try
{
System.out.println("Enter fair of ticket");
fare=sc.nextInt();
System.out.println("Wait your ticket booking is in process");
for(int i=0;i<8;i++)
{
System.out.print("..\t");
Thread.sleep(500);
}
}
catch (Exception e)
{
System.out.println(e);
}
System.out.println("\nYour Ticket has been confirmed Successfully..");
}
}
class Passenger extends Thread
{
// Reference variable of type Line.
TicketBooking ticket;
Passenger(TicketBooking ticket)
{
this.ticket = ticket;
}
@Override
public void run()
{
ticket.bookTicket();
}
}
class BookTicket
{
public static void main(String[] args)
{
TicketBooking obj = new TicketBooking();
// we are creating two threads which share
// same Object.
Passenger p1 = new Passenger(obj);
Passenger p2 = new Passenger(obj);
// both passengers will try to book ticket at same time .
p1.start();
p2.start();
}
}
/*
OUTPUT:
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>javac BookTicket.java
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>java BookTicket
Enter fair of ticket
550
Wait your ticket booking is in process
.. .. .. .. .. .. .. ..
Your Ticket has been confirmed Successfully.
Enter fair of ticket
600
Wait your ticket booking is in process
.. .. .. .. .. .. .. ..
Your Ticket has been confirmed Successfully.
D:\Sudhir\A.Y. 2022-23 EVEN SEM\JAVA_FE\Java Programs\Experiment No 11>
*/