1.5k views
4 votes
Write a program that will add the content of two counters every 45 seconds and place the result in an integer register.

User Kzfabi
by
4.9k points

1 Answer

5 votes

Answer:

Step-by-step explanation:

The following code is written in Java and runs a thread every 45 seconds that adds the two counters together and saves them in an integer variable called register. Then prints the variable. If this code runs 5 times it automatically breaks the loop. This can be changed or removed by removing the breakLoopCounter variable.

public static void add_Counters(int counterOne, int counterTwo) {

int register = 0;

int breakLoopCounter = 0;

try {

while (true) {

register += counterOne + counterTwo;

System.out.println(register);

Thread.sleep(45000);

breakLoopCounter += 1;

if (breakLoopCounter == 5) {

break;

}

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

User InvalidArgument
by
5.0k points