Multiple threads

Report a typo

Here's a class that extends the Thread class.

class WorkerThread extends Thread {
    
    @Override
    public void run() {
        // the method does something
    }
}

1) Create two instances of the given class and set the names like "worker-X", where X is any suffix (use the constructor to set the name).

2) Start the created threads. The method run of each instance must be executed in a new thread.

Note: the given class will be added to your solution automatically.

Write a program in Java 17
public class Main {
public static void main(String[] args) {
// create instances and start threads here
}
}

// Don't change the code below
class WorkerThread extends Thread {
private static final int NUMBER_OF_LINES = 3;

public WorkerThread(String name) {
super(name);
}

@Override
public void run() {
final String name = Thread.currentThread().getName();

if (!name.startsWith("worker-")) {
return;
}

for (int i = 0; i < NUMBER_OF_LINES; i++) {
System.out.println("do something...");
}
}
}
___

Create a free account to access the full topic