Shreyas arya

Generation

generate functionThu, 09 Feb 2023

string = shreyas delay = 5 seconds print string Number of retries = 3 if total duration of all attempts exceed 10 seconds then throw execption

import java.util.concurrent.TimeUnit; public class Solution { public static void main(String[] args) { String str = "shreyas"; int delay = 5; int maxRetries = 3; int totalDuration = 10; printWithRetry(str, delay, maxRetries, totalDuration); } public static void printWithRetry(String str, int delay, int maxRetries, int totalDuration) { int attempt = 0; int totalDelay = 0; while (attempt < maxRetries) { attempt++; if (totalDelay >= totalDuration) { throw new RuntimeException(); } try { TimeUnit.SECONDS.sleep(delay); System.out.println(str); return; } catch (InterruptedException e) { totalDelay += delay; } } throw new RuntimeException(); } }

Questions about programming?Chat with your personal AI assistant