Method 1: Use Thread directly
// Create a thread object
Thread t = new Thread(){
@Override
public void run(a) {
// The task to be performed
log.debug("running"); }};// Give the thread a name
t.setName("t1");
// Start the thread
t.start();
Copy the code
Method 2: Use the Runnale interface to work with threads
// Create a task object
Runnable task2 = new Runnable() {
@Override
public void run(a) {
log.debug("hello"); }};// Parameter 1 is the task object;
// Parameter 2 is the thread name, recommended
Thread t2 = new Thread(task2, "t2");
t2.start();
Copy the code
Method 3: FutureTask works with Thread
// Create a task object
FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
@Override
public Integer call(a) throws Exception {
log.debug("running.....");
Thread.sleep(1000);
return 100; }});// Parameter 1 is the task object;
// Parameter 2 is the thread name, recommended
Thread t = new Thread(task, "t1");
t.start();
log.debug("The result is {}", task.get());
Copy the code
conclusion
The first method overwrites the run method and the second method calls the run method of the Runnable interface. The internal logic of the Thread run method is to check whether the Runable interface is empty. If the Runable interface is not empty, the run method of the interface is called. Otherwise, the Thread run method is used by default. Method two realizes the separation of thread and task and has good reuse. The FutureTask
class implements the RunnableFuture
interface. The RunnableFuture
interface inherits the Runnable and Future
interfaces. Therefore, threads can be created using FutureTask
.