0% found this document useful (0 votes)
24 views5 pages

Oop Set 6

The document discusses four programming problems related to multithreading and concurrency. It includes code snippets for programs that use threads to find prime numbers in a range, demonstrate thread priority levels, use an executor framework for multitasking, and implement the producer-consumer problem.

Uploaded by

adada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Oop Set 6

The document discusses four programming problems related to multithreading and concurrency. It includes code snippets for programs that use threads to find prime numbers in a range, demonstrate thread priority levels, use an executor framework for multitasking, and implement the producer-consumer problem.

Uploaded by

adada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Enrollment No.

200170107069 Practical 6

Name: Patel Aditi Mukeshkumar


Enrollment No.: 200170107069
Subject: Object Orientated Programming

Practical : 6
Practical 6.1)
Write a Program for multithreading that finds prime number in the range provided by
user in command line argument depending upon range creates sufficient number of
child thread.

import java.util.*; public class Practical6_01 {


import java.lang.Thread; public static void main(String[] args) {
class PrimeInRange extends Thread{ Scanner sc = new Scanner(System.in);
int start,end; System.out.println("Enter the starting and
PrimeInRange(int start, int end){ ending number of range.");
this.start = start; int start = sc.nextInt();
this.end = end; int end = sc.nextInt();
} System.out.print("Prime numbers in range " +
@Override start + " to " + end + " is :");
public void run(){ while (start <= end) {
for (int i = start; i <= end; i++) { if(start+10 <= end){
if(isPrime(i)) PrimeInRange p = new PrimeInRange(start,
System.out.print(i + " "); start+10);
} p.start();
} start += 11;
private boolean isPrime(int num) { }else{
for (int i = 2; i*i <= num; i++) { PrimeInRange p = new PrimeInRange(start,
if(num%i == 0) end);
return false; p.start();
} start = end+1;
return true; }
} }
} System.out.println();
sc.close();
}
}

Code:
Output:

Object Orientated Programming 1|Page


Enrollment No. 200170107069 Practical 6

Practical 6.2)
Write a program that demonstrate thread priority four threads each with a different priority
level then the other is started objects and not the behave of each Thread.
Code:

import java.lang.Thread; public class Practical6_02 {


class SayHello extends Thread{ public static void main(String[] args) {
int threadNo; SayHello s1 = new SayHello(1);
SayHello(int threadNo){ SayHello s2 = new SayHello(2);
this.threadNo = threadNo; SayHello s3 = new SayHello(3);
} SayHello s4 = new SayHello(4);
@Override s1.setPriority(Thread.MIN_PRIORITY);
public void run() { s2.setPriority(Thread.MAX_PRIORITY);
int i = 0; s3.setPriority(Thread.MIN_PRIORITY);
while (i < 10) { s4.setPriority(Thread.NORM_PRIORITY);
System.out.println("Hello, Thead No : " s1.start();
+ this.threadNo + " with priority " + s2.start();
Thread.currentThread().getPriority() + " is s3.start();
currently running."); s4.start();
i++; }
} }
} }

Output:

Object Orientated Programming 2|Page


Enrollment No. 200170107069 Practical 6

Practical 6.3)
Write a program that demonstrate use of Executor Framework in Multitasking.
Code:

import java.util.concurrent.Executors; public class Practical6_03 {


import public static void main(String[] args) {
java.util.concurrent.ThreadPoolExecutor; ThreadPoolExecutor ex =
class Task implements Runnable{
(ThreadPoolExecutor)
String name;
Executors.newFixedThreadPool(2);
public Task(String name){
this.name = name;
} for(int i = 0; i < 5; i++){
public String getName(){ Task task = new Task("Task: " + i);
return name; System.out.println("Created: " +
} task.getName());
@Override
ex.execute(task);
public void run() {
try { }
System.out.println("Executing:"+name); ex.shutdown();
Thread.sleep(1000); }
} catch (InterruptedException e) { }
System.out.println("Interrupt
Occurred.");
}
}
}
Object Orientated Programming 3|Page
Enrollment No. 200170107069 Practical 6

Output:

Practical 6.4)
Write a program for handling producer consumer problem.
Code:

class Shop{ }
private int materials; }
private boolean available = false; class Producer extends Thread{
public synchronized int get(){ private Shop Shop;
while (available == false){ private int number;
try { wait(); } public Producer(Shop c, int number){
catch (InterruptedException ie) {} Shop = c;
} this.number = number;
available = false; }
notifyAll(); @Override
return materials; public void run(){
} for (int i = 0; i < 10; i++)
public synchronized void put(int value){ {
while (available == true){ Shop.put(i);
try { wait(); } System.out.println("Produced value " +
catch (InterruptedException ie) {} this.number+ " put: " + i);
} try{
materials = value; sleep((int)(Math.random() * 100));
available = true; }
notifyAll(); catch (InterruptedException ie) {}
} }
} }
class Consumer extends Thread{ }
private Shop Shop; public class Practical6_04{
private int number; public static void main(String[] args){
public Consumer(Shop c, int number){ Shop c = new Shop();
Shop = c; Producer p1 = new Producer(c, 1);
Objectthis.number
Orientated =Programming
number; Consumer c1 = new Consumer(c, 4 | 1);
Page
} p1.start();
@Override c1.start();
Enrollment No. 200170107069 Practical 6

Output:

Object Orientated Programming 5|Page

You might also like