59.4k views
1 vote
To create a thread that executes tasks after a specified delay or at a specified interval, use the ________________ class

1 Answer

6 votes

Final answer:

To create a thread that executes tasks after a specified delay or at a specified interval, you can use the Timer class in Java.

Step-by-step explanation:

To create a thread that executes tasks after a specified delay or at a specified interval, you can use the Timer class in Java. The Timer class provides methods to schedule tasks to be executed at a fixed rate or with a specified delay. Here's an example:



import java.util.Timer;



import java.util.TimerTask;



public class MyTask extends TimerTask {



public void run() {



// Code to be executed



}



public static void main(String[] args) {



Timer timer = new Timer();



TimerTask task = new MyTask();



timer.scheduleAtFixedRate(task, 0, 1000); // Execute task every 1 second



}

User Jonp
by
8.6k points