Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java multi threading Buzzer program

//  Aim: a) Write a program to simulate a buzzer. The program should make use of the Thread class.
//          Create a class named Buzzer that extends the Thread class. Create an interface named
//          MonitorTime which contains a method setBuzzerTime() . Your Buzzer class should
//          implement this interface. Override setBuzzerTime() method to set the buzzer time delay in
//          milliseconds and the number of times the buzzer should be repeated. Also, include
//          methodsblowBuzzer to start the buzzer.

package com.College_Java_Lab.Exp_9_Threads.Record;

// Source code:
import java.util.Scanner;

interface MonitorTime{
    void setBuzzerTime(int time);
}

public class Buzzer extends Thread implements MonitorTime {

    @Override
    public void setBuzzerTime(int time) {
        try {
            Thread.sleep(time* 1000L);
        }
        catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }

    Buzzer( int no ) {
        System.out.println( no + " Buzzer started...");
        this.start();
    }

    static void methodsblowBuzzer (int n, int time ){
        System.out.println("Buzzer started...");
        for( int i = 1; i <= n; ++i ) {
            Buzzer buzz = new Buzzer( i );
            buzz.setBuzzerTime(time);
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of times buzzer should be repeated : ");
        int n = in.nextInt();
        System.out.print("Set Buzzer time delay in seconds : ");
        int time = in.nextInt();
        methodsblowBuzzer( n, time);
    }

}

// INPUT/OUTPUT:
//        Enter number of times buzzer should be repeated : 3
//        Set Buzzer time delay in seconds : 1
//        Buzzer started...
//        1 Buzzer started...
//        2 Buzzer started...
//        3 Buzzer started...
Comment

PREVIOUS NEXT
Code Example
Java :: actionevent java swing 
Java :: android notification addaction example 
Java :: codding loop 
Java :: opencv copy image java 
Java :: reversing an integer with overflow handled 
Java :: spring down load 
Java :: how many return values can a method hava java 
Java :: Java create an object of the static class Mammal 
Java :: get cursor position in textarea java 
Java :: make pattern for V in jaca 
Java :: what are construtcor java 
Java :: Algorithms - transformation 
Java :: java random number generator 6 
Java :: Java Access Specifier in Overriding 
Java :: java list to jsonelement 
Java :: mock a service when the method returning void 
Java :: intellij evaluate expression 
Java :: get alpha from image java 
Java :: how to write no in java 
Java :: text blocks 
Java :: Java socket connect to gmail 
Java :: java Optional to Collection 
Java :: java codigo para criar um aleatorio entre valores 
Java :: JAVA Print Concatenated Strings 
Java :: node constructor 
Java :: java program scan folder find files using time 
Java :: merge sort algorithm in java short answer 
Java :: java 8 anymatch two lists 
Java :: flutter calculate sum in a list 
Java :: ArrayList go to value jav 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =