8-(1)
Write a program to find
prime number in given
range using both method of multithreading.
package com.company;import static com.company.primeclass.primes; public class Main {int counter = 1; static int N;public void printOddNumber() { synchronized (this) {while (counter < N) {while (counter % 2 == 0) { try {wait();}catch (InterruptedException e) { e.printStackTrace();}}System.out.println(counter +") " +primes.get(counter) + " by even thread"); counter++;notify();}}}public void printEvenNumber() { synchronized (this){while (counter < N) {while (counter % 2 == 1) { try {wait();}catch (InterruptedException e) { e.printStackTrace();}}System.out.println(counter +") " +primes.get(counter)+ " by odd thread"); counter++;notify();}}}public static void main(String[] args) { primeclass.functionprime();N = 10;Main mt = new Main();Thread t1 = new Thread(new Runnable() { public void run(){mt.printEvenNumber();}});Thread t2 = new Thread(new Runnable() { public void run(){mt.printOddNumber();}});t1.start();t2.start();}}import java.util.ArrayList; public class primeclass {static int MAX_SIZE = 1000005; static ArrayList<Integer> primes =new ArrayList<Integer>(); static void functionprime() {boolean[] IsPrime = new boolean[MAX_SIZE]; for (int i = 0; i < MAX_SIZE; i++)IsPrime[i] = true;for (int p = 2; p * p < MAX_SIZE; p++) { if (IsPrime[p] == true) {for (int i = p * p; i < MAX_SIZE; i += p) IsPrime[i] = false;}}for (int p = 2; p < MAX_SIZE; p++) if (IsPrime[p] == true)primes.add(p);}}
8-(2)
Write a program to find
prime number in given
range using both method of multithreading.
Assume one class
Queue that defines
queue of fix size says 15.
●
Assume one class producer
which implements Runnable, having priority NORM_PRIORITY +1
● One more class consumer implements Runnable, having priority
NORM_PRIORITY-1
●
Class TestThread is having main method with maximum priority, which creates 1 thread for producer and 2
threads for consumer.
● Producer
produces number of elements and put on the queue.
when queue becomes
full it notifies other threads.
Consumer consumes number of elements and notifies other thread when queue become
empty.
package com.company;import static java.lang.Thread.NORM_PRIORITY; public class Main {public static void main(String[] args) throws InterruptedException {final Producer_consumer pc = new Producer_consumer(); Thread t1 = new Thread(new Runnable() {@Overridepublic void run() { try {pc.produce();} catch (InterruptedException e) { e.printStackTrace();}}});Thread t2 = new Thread(new Runnable() { @Overridepublic void run() { try {pc.consume();} catch (InterruptedException e) { e.printStackTrace();}}});Thread t3 = new Thread(new Runnable() { @Overridepublic void run() { try {pc.consume();} catch (InterruptedException e) { e.printStackTrace();}}});t1.setPriority(NORM_PRIORITY +1);t2.setPriority(NORM_PRIORITY -1);t3.setPriority(NORM_PRIORITY -1); t1.start();t2.start();t3.start();t1.join();t2.join();t3.join();}}import java.util.LinkedList;public class Producer_consumer { LinkedList<Integer> list = new LinkedList<>(); int capacity = 2;public void produce() throws InterruptedException { int value = 0;while (true) { synchronized (this) {while (list.size() == capacity) wait();System.out.println("Producer produced-" + value); list.add(value++);notify(); Thread.sleep(1000);}}}public void consume() throws InterruptedException { while (true) {synchronized (this) { while (list.size() == 0)wait();int val = list.removeFirst();System.out.println("Consumer consumed-"+ val); notify();Thread.sleep(1000);}}}}
0 Comments