Program 3 Threads Demo

Write  a  Java  program  that  implements  a  multi-thread  application  that  has  three threads.  First  thread  generates  a  random  integer  for  every  1  second;  second thread  computes  the  square  of  the  number  and prints;  third  thread  will  print  the value of the cube of the number.

1  package edu.practice.java.threading;
2 
3  public class NumberGenerator implements Runnable {
4 
5          private Integer number;
6          private int count;
7          private int pendingConsumption = 0;
8 
9          public NumberGenerator(int count) {
10                 this.count = count;
11         }
12 
13         @Override
14         public void run() {
15                 for (int i = 0; i < this.count; i++) {
16                         try {
17                                 this.generate();
18                                 Thread.sleep(1000);
19                         } catch (InterruptedException e) {}
20                 }
21         }
22 
23         private synchronized void generate() {
24                 this.number = (int) (Math.random() * 20);
25                 this.pendingConsumption = 2; // Now I know how many consumers I have
26                 System.out.println("Generated number:" + this.number);
27                 this.notify();
28         }
29 
30         public synchronized Integer get() {
31                 while (this.pendingConsumption == 0) {
32                         try {
33                                 wait();
34                         } catch (InterruptedException e) {}
35                 }
36                 this.pendingConsumption--;
37                 return this.number;
38         }
39 
40 }
41 
42 package edu.practice.java.threading;
43 
44 public class Squarer implements Runnable {
45         
46         private NumberGenerator numberGenerator;
47         private int count;
48 
49         public Squarer(NumberGenerator numberGenerator, int count) {
50                 this.count = count;
51                 this.numberGenerator = numberGenerator;
52         }
53 
54         @Override
55         public void run() {
56                 for (int i = 0; i < count; i++) {
57                         try {
58                                 Integer number = this.numberGenerator.get();
59                                 System.out.println("Square of " + number + " is " + (number * number));
60                                 Thread.sleep(1000);
61                         } catch (InterruptedException e) {}
62                 }
63         }
64 
65 }
66 
67 package edu.practice.java.threading;
68 
69 public class Cuber implements Runnable {
70 
71         private NumberGenerator numberGenerator;
72         private int count;
73 
74         public Cuber(NumberGenerator numberGenerator, int count) {
75                 this.count = count;
76                 this.numberGenerator = numberGenerator;
77         }
78 
79         @Override
80         public void run() {
81                 for (int i = 0; i < count; i++) {
82                         try {
83                                 Integer number = this.numberGenerator.get();
84                                 System.out.println("Cube of " + number + " is " + (number * number * number));
85                                 Thread.sleep(1000);
86                         } catch (Exception e) {}
87                 }
88         }
89 
90 }
91 
92 package edu.practice.java.threading;
93 
94 public class ThreeMusketeers {
95 
96         public static void main(String[] args) {
97                 System.out.println("Let's play");
98                 final int count = 5;
99                 NumberGenerator numberGenerator = new NumberGenerator(count);
100                Thread generator = new Thread(numberGenerator);
101                Thread squarer = new Thread(new Squarer(numberGenerator, count));
102                Thread cuber = new Thread(new Cuber(numberGenerator, count));
103                
104                generator.start();
105                squarer.start();
106                cuber.start();
107        }
108
109}


OUTPUT:

Let's play

Generated number:9

Cube of 9 is 729

Square of 9 is 81

Generated number:12

Cube of 12 is 1728

Square of 12 is 144

Generated number:15

Cube of 15 is 3375

Generated number:8

Square of 8 is 64

Cube of 15 is 3375

Generated number:14

Cube of 14 is 2744

Square of 14 is 196


No comments:

Post a Comment

Online Certifications

Python for Beginners Offered by: Christian Drumm, Stephan Jacobs Course dates: 2022-04-05 to 2022-06-01 Topics Python Fundamentals Lists and...