Handling interruptions

Report a typo

The class CounterThread extends Thread and overrides the run method. The method increments the counter variable in a loop. As it is now, the loop works infinitely. You should fix it by adding interruption handling.
If another thread interrupts this thread, it must print "It was interrupted" and stop.
Please, do not remove the loop. Otherwise, your solution may not pass tests.

Write a program in Java 17
class CounterThread extends Thread {

@Override
public void run() {
long counter = 0;

while (true) {
counter++;
}
}
}
___

Create a free account to access the full topic