0% found this document useful (0 votes)
18 views

Threading

Uploaded by

sriram37357
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Threading

Uploaded by

sriram37357
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Ex.

No:12
12.12.2024
MULTITHREADING

1) Write a multithreading program for the following scenario. Create five threads. First thread
should calculate factorial of a number. Thread2 should print welcome world 100 times. Thread 3
should check whether the given number is odd or even. Thread 4 finds maximum of three
numbers. Thread 5 swaps 2 values without using a temporary variable. The inputs of the above
methods should be passed from the main method. The priorities are as follows: Thread1-6;
Thread 2-max priority; Thread 3 –min priority; Thread 4 2 more than thread1 and thread 5 4
more than thread 3
Source code:
import java.util.*;
class A extends Thread{
int a;
A(int a){
this.a = a;
}
public void run(){
int factorial=1;
for(int i=1;i<=a;i++){
factorial*=i;
}
System.out.println(factorial);
}
}
class B extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.println("Welcome world");
}
}
}
class C extends Thread{
int a;

Soma Prasanth
23I362
C(int a){
this.a = a;
}
public void run(){
if(a%2==0){
System.out.println("Even");
}
else{
System.out.println("Odd");
}
}
}
class D extends Thread{
int[] a;
D(int[] a){
this.a = a;
}
public void run(){
Arrays.sort(a);
System.out.println(a[a.length-1]);
}
}
class E extends Thread{
int a,b;
E(int a,int b){
this.a = a;
this.b = b;
}
public void run(){
a = a+b;
b = a-b;
a = a-b;

Soma Prasanth
23I362
System.out.printf("Values after swapping:\n a: %d b: %d",a,b);
}
}
class Main{
public static void main(String[] args){
Scanner ip = new Scanner(System.in);
int maxPriority = 10;
int minPriority = 1;
int a = ip.nextInt();
A t1 = new A(a);
B t2 = new B();
C t3 = new C(a);
int[] arr = new int[3];
for(int i=0;i<3;i++){
arr[i]=ip.nextInt();
}
D t4 = new D(arr);
a = ip.nextInt();
int b=ip.nextInt();
E t5 = new E(a,b);
t1.setPriority(6);
t2.setPriority(maxPriority);
t3.setPriority(minPriority);
t4.setPriority(2+t1.getPriority());
t5.setPriority(4+t3.getPriority());
t1.start();t2.start();t3.start();t4.start();t5.start();
}
}
Output:

Soma Prasanth
23I362
2) Write a program for illustrating synchronization. Create three threads each of which prints
the string shown
[
Java
]
Source code:
class Mythread extends Thread{
public void run(){
System.out.println("[");
System.out.println("Java");
System.out.println("]");
}
}
class threading{
public static void main(String[] args) {
Mythread t1 = new Mythread();
Mythread t2 = new Mythread();
Mythread t3 = new Mythread();
t1.start();t2.start();t3.start();

Soma Prasanth
23I362
try{
t1.join();
t2.join();
t3.join();
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
Output:

3) Write an application that gets 10 number as input. Compute the following using
multithreading.
1. Prime number
2. Even and odd numbers
3. Ascending order
4. Decesnding order
Use appropriate milliseconds to make the threads sleep. Make use of join() method to enable the
main to wait for the child threads.
Source code:
import java.util.*;
class prime extends Thread{
int[] arr;
prime(int[] arr){
Soma Prasanth
23I362
this.arr = arr;
}
static boolean checkPrime(int n){
if(n==1){
return false;
}
for(int i =2;i<n;i++){
if(n%i==0){
return false;
}
}
return true;
}
public void run(){
ArrayList<Integer> prime = new ArrayList<>();
for(int i=0;i<arr.length;i++){
if(checkPrime(arr[i])){
prime.add(arr[i]);
}
}
System.out.println(prime);
}
}
class EvenOdd extends Thread{
int[] arr;
EvenOdd(int[] arr){
this.arr = arr;
}
public void run(){
ArrayList<Integer> even = new ArrayList<>();
ArrayList<Integer> odd = new ArrayList<>();
for(int i=0;i<arr.length;i++){

Soma Prasanth
23I362
if(arr[i]%2==0){
even.add(arr[i]);
}
else{
odd.add(arr[i]);
}
}
System.out.println("Even numbers are: "+even);
System.out.println("Odd numbers are: "+odd);
}
}
class AscendingOrder extends Thread{
int[] arr;
AscendingOrder(int[] arr){
this.arr = arr;
}
public void run(){
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
class DescendingOrder extends Thread{
int[] arr;
DescendingOrder(int[] arr){
this.arr = arr;
}
public void run(){
Arrays.sort(arr);
int i=0,j=arr.length-1;
while(i<j){
int temp=arr[i];
arr[i]=arr[j];

Soma Prasanth
23I362
arr[j]=temp;
i++;
j--;
}
System.out.println(Arrays.toString(arr));
}
}
class threading{
public static void main(String[] args){
Scanner ip=new Scanner(System.in);
int[] arr=new int[10];
for(int i=0;i<10;i++) arr[i]=ip.nextInt();
prime t1 = new prime(arr);
EvenOdd t2 = new EvenOdd(arr);
AscendingOrder t3 = new AscendingOrder(arr);
DescendingOrder t4 = new DescendingOrder(arr);
t1.start();
try{
t1.join();
}catch(InterruptedException ie){
ie.printStackTrace();
}
t2.start();
try{
t2.join();
}catch(InterruptedException ie){
ie.printStackTrace();
}
t3.start();
try{
t3.join();
}catch(InterruptedException ie){

Soma Prasanth
23I362
ie.printStackTrace();
}
t4.start();
try{
t4.join();
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
Output:

4)

Source code:
import java.util.*;

class Account extends Thread{


String accNo,accName;

Soma Prasanth
23I362
Double balance,interest;
Account(String no,String name,double balance){
accNo = no;
accName = name;
this.balance = balance;
}
public void run(){
interest = (balance*0.15);
balance+=interest;
System.out.printf("interest : %.2f\nbalance : %.2f\n",balance,interest);
}
}
class Main{
public static void main(String[] args) {
Scanner ip = new Scanner(System.in);
String accno1 = ip.nextLine();
String name1 = ip.nextLine();
double balance1 = ip.nextDouble();
ip.nextLine();
String accno2 = ip.nextLine();
String name2 = ip.nextLine();
double balance2 = ip.nextDouble();
Account a1 = new Account(accno1, name1, balance1);
Account a2 = new Account(accno2, name2, balance2);
a1.start();a2.start();
}
}
Output:

Soma Prasanth
23I362
5)

Source code:
import java.util.*;

class Frequency extends Thread{


String s;
Frequency(String s){
this.s = s;
}
public void run(){
HashMap<Character, Integer> freq = new HashMap<>();
for(char ch :s.toCharArray()){
if(freq.containsKey(ch)){
freq.put(ch,freq.get(ch)+1);
Soma Prasanth
23I362
}
else{
freq.put(ch,1);
}
}
for(char key : freq.keySet()){
System.out.println(key+""+freq.get(key));
}
System.out.println();
}
}
class Main{
public static void main(String[] args) {
Scanner ip = new Scanner(System.in);
int n = ip.nextInt();
ip.nextLine();
String[] arr = new String[n];
for(int i=0;i<n;i++){
arr[i]=ip.nextLine();
}
for(int i=0;i<n;i++){
Frequency f = new Frequency(arr[i]);
f.start();
try{
f.join();
}catch(InterruptedException ie){
ie.printStackTrace();
}
}

}
}

Soma Prasanth
23I362
Output:

Soma Prasanth
23I362

You might also like