GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — java-programs/Multithread_Application/multithread.java at main · IntScription/java-programs Expand file tree/ Copy pathmultithread.javaMore file actions 90 lines (73 loc) · 2.11 KB / Copy pathmultithread.javaFile metadata and controls90 lines (73 loc) · 2.11 KB 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 /* Write a Java program that implements a multithreaded 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 the cube of the number. */ package Multithread_Application; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Square implements Runnable { private int num; Square(int n) { num = n; } public void printSquare() { System.out.println("Square of " + num + " is " + (num * num)); } public void run() { printSquare(); } } class Cube implements Runnable { private int num; Cube(int n) { num = n; } public void printCube() { System.out.println("Cube of " + num + " is " + (num * num * num)); } public void run() { printCube(); } } class RandInt extends Thread { private final ExecutorService executor = Executors.newFixedThreadPool(2); public void printRandom() throws InterruptedException { Random rd = new Random(); while (true) { int rnum = rd.nextInt(10); System.out.println("Random Number is " + rnum); Thread.sleep(3000); executor.submit(new Square(rnum)); executor.submit(new Cube(rnum)); } } public void run() { try { printRandom(); } catch (InterruptedException e) { } } public void shutdown() { executor.shutdown(); } public void awaitTermination() throws InterruptedException { executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } } public class multithread { public static void main(String[] args) { RandInt ob1 = new RandInt(); ob1.start(); try { Thread.sleep(15000); // Run for 15 seconds ob1.shutdown(); ob1.awaitTermination(); } catch (InterruptedException e) { e.printStackTrace(); } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |