evelop a Java program that simulates a simple countdown timer usingmultithreading. The program should allow users to start multiple timersconcurrently, each running independently and provide its sample input,output
Question
evelop a Java program that simulates a simple countdown timer usingmultithreading. The program should allow users to start multiple timersconcurrently, each running independently and provide its sample input,output
Solution
Sure, here is a simple Java program that simulates a countdown timer using multithreading.
import java.util.concurrent.*;
public class CountdownTimer implements Runnable {
private final int timeToCount;
public CountdownTimer(int timeToCount) {
this.timeToCount = timeToCount;
}
@Override
public void run() {
try {
for (int i = timeToCount; i >= 0; i--) {
System.out.println(Thread.currentThread().getName() + ": " + i);
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new CountdownTimer(10));
executorService.execute(new CountdownTimer(5));
executorService.shutdown();
}
}
In this program, we have a CountdownTimer class that implements Runnable. This class has a timeToCount field that represents the time to count down from. The run method counts down from timeToCount to 0, printing the current count every second.
In the main method, we create an ExecutorService to manage our threads. We then start two countdown timers, one that counts down from 10 and one that counts down from 5. These timers run concurrently, each in their own thread.
The ExecutorService is then shut down, which will cause the program to exit once all timers have finished.
The sample input would be the time to count down from, which is passed to the CountdownTimer constructor. The output would be the countdown printed to the console.
Similar Questions
Se quiere implementar un monitor Java que proporcione la funcionalidad básica del CountDownLatch, con los métodos countDown y await.public class MiCountDownLatch { private int n; public MiCountDownLatch(int n0) { n=n0; } public synchronized void countDown() { n--; notifyAll(); } public void await() throws InterruptedException { while (n > 0) wait(); }}Preguntas 14 de 20 0,5 Puntos. Puntos descontados por fallo: 0.25Esta solución no es correcta porque se pueden producir condiciones de carrera.VerdaderoFalsoBorra selecciónPreguntas 15 de 20 0,5 Puntos. Puntos descontados por fallo: 0.25Con esta solución, como el valor del contador de la barrera podría ser negativo, se podrían quedar hilos suspendidos al hacer await sobre la barrera, aunque la barrera esté ya abierta.VerdaderoFalsoBorra selecciónPreguntas 16 de 20 0,5 Puntos. Puntos descontados por fallo: 0.25Para que esta solución sea correcta hay que sustituir en el método countDown la línea actual de notifyAll() por signalAll().VerdaderoFalso
Develop a Java program that creates a GUI application for a stopwatch usingSwing. The stopwatch should have start, stop, and reset buttons, and displaythe elapsed time.
What is the purpose of the CountDownLatch class in Java threads? Question 3Answera.It Delays the execution of a thread.b.It Counts the number of active threads in the program.c.It synchronizes the execution of multiple threads till the operations are complete.d.Allows one or more threads to wait for a set of operations to complete.
Develop a program to simulate the countdown sequence for a historic rocket launch. Given a positive integer n representing the number of seconds left, the program should print a countdown sequence in the format "n-(n-1)-...-1". This countdown replicates the final moments before the liftoff of a significant space mission. Write a recursive function called countdown that takes n as input and prints the countdown sequence.Input format :The input consists of an integer n, representing the number of seconds left for the rocket launch.Output format :The output prints the countdown sequence in the format: "n-(n-1)-...-1".
Single File Programming QuestionProblem StatementDevelop a program to simulate the countdown sequence for a historic rocket launch. Given a positive integer n representing the number of seconds left, the program should print a countdown sequence in the format "n-(n-1)-...-1". This countdown replicates the final moments before the liftoff of a significant space mission. Write a recursive function called countdown that takes n as input and prints the countdown sequence.Input format :The input consists of an integer n, representing the number of seconds left for the rocket launch.Output format :The output prints the countdown sequence in the format: "n-(n-1)-...-1".Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:5 ≤ n ≤ 100Sample test cases :Input 1 :5Output 1 :5-4-3-2-1Input 2 :13Output 2 :13-12-11-10-9-8-7-6-5-4-3-2-1Input 3 :100Output 3 :100-99-98-97-96-95-94-93-92-91-90-89-88-87-86-85-84-83-82-81-80-79-78-77-76-75-74-73-72-71-70-69-68-67-66-65-64-63-62-61-60-59-58-57-56-55-54-53-52-51-50-49-48-47-46-45-44-43-42-41-40-39-38-37-36-35-34-33-32-31-30-29-28-27-26-25-24-23-22-21-20-19-18-17-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.