Object Oriented Programming: Unit III: Exception Handling and Multithreading

Multithreading

with Example Java Programs

The multiple threads can be created both by extending Thread class and by implementing the Runnable interface.

Multithreading

The multiple threads can be created both by extending Thread class and by implementing the Runnable interface.

1. Java Program for creating multiple threads by extending Thread Class

class A extends Thread

{

public void run()

{

for(int i=0;i<=5;i++)//printing 0 to 5

{

System.out.println(i);

}

}

}

class B extends Thread

{

public void run()

{

for(int i=10;i>=5;i--)//printing 10 to 5

{

System.out.println(i);

}

}

}

class ThreadProg

{

public static void main(String args[])

{

A t1=new A();

B t2=new B();

t1.start();

t2.start();

}

}

Output

0

1

2

3

4

5

10

9

8

7

6

5

2. Java Program for creating multiple threads by implementing the Runnable interface

class A implements Runnable

{

public void run()

{

for(int i=0;i<=5;i++)//printing 0 to 5

{

System.out.println(i);

}

}

}

class B implements Runnable

{

public void run()

{

for(int i=10;i>=5;i--)//printing 10 to 5

{

System.out.println(i);

}

}

}

class ThreadProg Runn

{

public static void main(String args[])

{

A obj1=new A();

B obj2=new B();

Thread t1=new Thread(obj1);

Thread t2=new Thread(obj2);

t1.start();

t2.start();

}

}

Output

0

1

2

3

4

5

10

9

8

7

6

5.

Ex. 3.13.1: Write a Java application program for generating four threads to perform the following operations - i) Getting N numbers as input ii) Printing the numbers divisible by five iii) Printing prime numbers iv) Computing the average.

Sol. :

import java.io.*;

import java.util.*;

class FirstThread extends Thread

{

public void run() //generating N numbers

{

int i;

System.out.println("\nGenerating Numbers: ");

for (i=1;i<=10;i++)

{

System.out.println(i);

}

}

}

class SecondThread extends Thread

{

public void run() //Displaying the numbers divisible by five

{

int i;

System.out.println("\nDivisible by Five: ");

for (i=1;i<=10;i++) //10 can be replaced by any desired value

{

if (i%5==0)

System.out.println(i);

}

}

}

class ThirdThread extends Thread

{

public void run() //generating the prime numbers

{

int i;

System.out.println("\n Prime Numbers: ");

for (i=1;i<=10;i++) //10 can be replaced by any desired value

{

int j;

for (j=2; j<i; j++)

{

int n = i%j;

if (n==0)

break;

}

if(i == j)

System.out.println(i);

}

}

}

class FourthThread extends Thread

{

public void run() //generating the prime numbers

{

int i,sum;

double avg;

sum=0;

System.out.println("\nComputing Average: ");

for (i=1;i<=10;i++) //10 can be replaced by any desired value

sum=sum+i;

avg=sum/(i-1);

System.out.println(avg);

}

}

class MainThread

{

public static void main(String[] args) throws IOException

{

FirstThread T1 = new FirstThread(); //creating first thread

SecondThread T2 = new SecondThread(); //creating second thread

ThirdThread T3 = new ThirdThread(); //creating Third thread

FourthThread T4 = new FourthThread(); //creating Fourth thread

T1.start(); //First Thread starts executing

T2.start();//Second Thread starts executing

T3.start();//Third Thread starts executing

T4.start();//Fourth Thread starts executing

}

}

Output

Generating Numbers:

1

2

3

4

5

6

7

8

9

10

Divisible by Five:

5

10

Computing Average:

5.0

Prime Numbers:

2

3

5

7

Ex. 3.13.2: Create a Bank Database application program to illustrate the use of multithreads

Sol. :

public class BankAppl implements Runnable {

private Account acc = new Account();

public static void main(String[] args) {

BankAppl obj = new BankAppl();

Thread t1 = new Thread(obj);

Thread t2 = new Thread(obj);

Thread t3= new Thread(obj);

t1.setName("Mr.XYZ");

t2.setName("Mr.ABC");

t3.setName("Mr.POR");

t1.start();

t2.start()

t3.start();

}

public void run() {

for (int x = 0; x < 10; x++) {

makeWithdrawal(10);

if (acc.getBalance() < 0) {

System.out.println("Account Overdrawn!!!");

}

}

}

void makeWithdrawal(int amt) {

int bal;

bal acc.getBalance();

if (bal >= amt) {

System.out.println("\t"+Thread.currentThread().getName()+withdraws Rs."+amt);

try {

Thread.sleep(100);

} catch (InterruptedException ex) {

}

acc.withdraw(amt);

bal-acc.getBalance();

System.out.println("\t. The Balance: "+bal);

} else {

System.out.println("Insufficient Balance in account for " + Thread.currentThread().getName() + " to withdraw " + acc.getBalance());

}

}

}

class Account {

private int balance 100;

public int getBalance() {

return balance;

}

public void withdraw(int amount) {

balance = balance - amount;

}

}

Review Questions

1. What is multithreading? What are the methods available in Java for inter-thread communication? Discuss with example

2.Write a Java program to illustrate multithreaded programming.

3.Write notes on multi-threaded programming.

4. Write a java application that illustrate the use of multithreading. Explain the same with sample input.

5. Describe the creation of a single thread and multiple threads using an example

6.Create a simple real life application program in Java to illustrate the use of multithreads.

Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : with Example Java Programs - Multithreading