Follow these steps to implement the example:
- Create a class named Task that implements the Runnable interface:
public class Task implements Runnable {
- Declare a private long attribute named milliseconds:
private final long milliseconds;
- Implement the constructor of the class to initialize its attribute:
public Task (long milliseconds) { this.milliseconds=milliseconds; }
- Implement the run() method. Put the thread to sleep for the number of milliseconds specified by the milliseconds attribute:
@Override public void run() { System.out.printf("%s: Begin\n", Thread.currentThread().getName()); try { TimeUnit.MILLISECONDS.sleep(milliseconds); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("%s: ...