Oop Set 6
Oop Set 6
200170107069 Practical 6
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.
Code:
Output:
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:
Output:
Practical 6.3)
Write a program that demonstrate use of Executor Framework in Multitasking.
Code:
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: