Starting threads

Report a typo

You have a class that implements the Runnable interface.

class RunnableWorker implements Runnable {
    
    @Override
    public void run() {
        // the method does something
    }
}

1) Create three threads using instances of RunnableWorker. Set the names of these threads to "worker-X", where X is any suffix. Use the Thread(Runnable target, String name) constructor to pass a thread name on thread creation.

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

Note: you don't need to rewrite the given class; it will be added to your solution automatically.

Write a program in Java 17
public class Main {

public static void main(String[] args) {

// create threads and start them using the class RunnableWorker
}
}

// Don't change the code below
class RunnableWorker implements Runnable {

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

if (name.startsWith("worker-")) {
System.out.println("too hard calculations...");
} else {
return;
}
}
}
___

Create a free account to access the full topic