Interrupting a thread

Report a typo

You are dealing with the class Worker that extends Thread . The class overrides the method run to do something useful. You need to start an instance of this class (in a new thread), wait for it a little (2000-3000 milliseconds) and interrupt this new thread.

If you do not interrupt the thread after starting, something bad will happen.

Write a program in Java 17
class InterruptedExample {

private static long mainThreadId = Thread.currentThread().getId();

public static void main(String[] args) throws InterruptedException {

Worker worker = new Worker();

// write your code here
}

// Don't change the code below
static class Worker extends Thread {

@Override
public void run() {

try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
throw new RuntimeException("You need to wait longer!", e);
}

final long currentId = Thread.currentThread().getId();

if (currentId == mainThreadId) {
throw new RuntimeException("You must start a new thread!");
}

while (true) {
if (isInterrupted()) {
System.out.println("Interrupted");
break;
}
}
}
___

Create a free account to access the full topic