Question

In: Computer Science

java language question: Write a thread that continuously prints a message every 100 milliseconds while it...

java language question:

Write a thread that continuously prints a message every 100 milliseconds while it is still alive. The thread should be written in such a way that it can be terminated at any time by a control program (main).

Solutions

Expert Solution

-> For End Program press q and Enter
-> You can change delay time by changing the delay variable(Default 100 ms)
-> Don't Forget to check if the thread is null before call interrupt
RESULT:


CODE (Text File Also Included In End):


CODE:

myThread.java

import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class myThread {

    // Initilizing All Variables
    private static int delay = 100;
    private static Runnable r;
    private static Boolean stop = false;
    private static Thread t;
    private static Scanner scanner;

    //Main Method
    public static void main(String[] args) {

        // Creating Runnable to handle print process
        Runnable r = new Runnable() {
            @Override
            public void run() {
                while (!stop) {
                    System.out.println("Thread is Running");
                    // delaying Thread
                    try {
                        t.sleep(delay);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(myThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                // Stopping Thread
                if (t != null) {
                    t.interrupt();
                }
            }
        };

        //Creating Thread using Runnable
        Thread t = new Thread(r);
        t.start();

        scanner = new Scanner(System.in);
        // Loop for exit from Program
        while (true) {
            String val = scanner.next();
            // When Entered q Exit From Program
            if (val.equals("q")) {
                stop = true;
                break;
            }
        }
    }

}


Related Solutions

PUT IN PYTHON LANGUAGE CODE # Write one while-loop that starts at 500 and prints every...
PUT IN PYTHON LANGUAGE CODE # Write one while-loop that starts at 500 and prints every 6th number down to 300 # (i.e., prints 500, 494, 488, . . . etc., but does not print any number lower than 300). # Write one while-loop that starts at 80 and prints every 12h number thereafter, # but does not print any number greater than 210 # Write one while-loop that prints all the numbers from 30 through 70, # except for...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Question #2 Write a program that prints a nicely formatted table of the product of every...
Question #2 Write a program that prints a nicely formatted table of the product of every pair of numbers in a given set of integer numbers. Your program reads two integer numbers x and y that represent the lower and upper bounds of the set, (?≤?x≤y), respectively. Then, for every pair of numbers in this set {?,?+1,?+2,…,?}{x,x+1,x+2,…,y}, your program should compute the product of the two numbers. Please note the following: Your program should read each input (lower bound and...
java. please don't use complicated language I can follow up. Write a for loop that prints...
java. please don't use complicated language I can follow up. Write a for loop that prints the integers from 1 to 100, all on one line, space-separated. However, after printing 3 numbers, you need to skip the next number and print a counter in parenthesis. 1 2 3 (1) 5 6 7 (2) 9 10 11 (3) 13 14 15 (4) 17 18 19 [... and so on ...] Write this code once with for loop, once with while loop,...
every code should be in java Program 1: Write a while loop that sums the integers...
every code should be in java Program 1: Write a while loop that sums the integers from 1 to 10, excluding 3 and 6. Print the sum. [Hint: Use logical operators] Program 2: Write a for loop that attempts to display the numbers from 1 to 10, but terminates when the control variable reaches the value 6. Program 3: Write a do...while loop that prints the integers from 10 to 0, inclusive.
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
Need to write a c program Program prints a message telling the user to push a...
Need to write a c program Program prints a message telling the user to push a key to start the game. Once in game, output tells the player which key to push. The key to press should be determined randomly. Game runs continuously until the user reaches a loose condition. A wrong key is pressed
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT