×
NOTE!
Click on MENU to Browse between Subjects...
Advertisement
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
(Effective from the academic year 2018 -2019)
SEMESTER - IV
Course Code 18CSL47
CIE Marks 40
Number of Contact Hours/Week 0:2:2
SEE Marks 60
Total Number of Lab Contact Hours 36
Exam Hours 03
Experiments 3 B
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 cube of the number.
Advertisement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package threeB; import java.lang.Thread; import java.util.Random; class first extends Thread{ public void run() { int num=0; Random r = new Random(); try { num=r.nextInt(100); System.out.println("First Thread : The Number Generated is : "+num); Thread t2=new Thread(new second(num)); t2.start(); Thread t3=new Thread(new third(num)); t3.start(); Thread.sleep(1000); }catch(Exception e) { System.out.println(e.getMessage()); } } } class second implements Runnable { public int x; public second(int x) { this.x=x; } public void run() { System.out.println("Second Thread : Square of the number is :"+x*x); } } class third implements Runnable { int x; public third(int x) { this.x=x; } public void run() { System.out.println("Third Thread : Cube of the Number is :"+x*x*x); } } public class Multithread { public static void main(String[] args) { first a = new first(); a.start(); } } |
Advertisement
Fig 3.3: Output .
Advertisement
Fig 3.4: Output .
×
Note
Please Share the website link with Your Friends and known Students...
-ADMIN
-ADMIN
×
Note
Page Number is specified to navigate between Pages...
T = Text book
QB = Question Bank
AS = Amswer Script
-ADMIN
T = Text book
QB = Question Bank
AS = Amswer Script
-ADMIN
Advertisement