方式1:继承Thread类,然后重载run()方法
package thread;
import org.junit.jupiter.api.Test;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @program: springdemo
* @description: ThreadMode1
* @author: wangwenguan6
* @date: 2024-04-10 20:14:20
* @doc:
**/
public class ThreadMode1 {
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
public static void main(String[] args) {
ThreadMode1 thread = new ThreadMode1();
thread.new MyThread1().start();
thread.new MyThread1().start();
thread.new MyThread2().start();
}
class MyThread1 extends Thread {
@Override
public void run() {
//业务逻辑xxxx
System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName()+"启动");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName()+"执行完毕");
}
}
class MyThread2 extends Thread {
@Override
public void run() {
//业务逻辑xxxx
System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName()+"启动");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(sdf.format(new Date()) + "线程:" + Thread.currentThread().getName()+"执行完毕");
}
}
}
执行结果为:
2024-04-10 20:28:21.842 线程:Thread-1启动
2024-04-10 20:28:21.842 线程:Thread-2启动
2024-04-10 20:28:21.842 线程:Thread-0启动
2024-04-10 20:28:24.848线程:Thread-2执行完毕
2024-04-10 20:28:26.847 线程:Thread-1执行完毕
2024-04-10 20:28:26.847 线程:Thread-0执行完毕
方式2:实现Runnable接口
package thread;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @program: springdemo
* @description: ThreadMode2
* @author: wangwenguan6
* @date: 2024-04-10 20:31:15
* @doc:
**/
public class ThreadMode2 {
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
public static void main(String[] args) {
ThreadMode2 th = new ThreadMode2();
Thread1 thread1_1 = th.new Thread1();
Thread1 thread1_2 = th.new Thread1();
Thread2 thread2 = th.new Thread2();
new Thread(thread1_1).start();
new Thread(thread1_2).start();
new Thread(thread2).start();
}
public class Thread1 implements Runnable {
@Override
public void run() {
//业务逻辑xxxx
System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName() + "启动");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName() + "执行完毕");
}
}
public class Thread2 implements Runnable {
@Override
public void run() {
//业务逻辑xxxx
System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName() + "启动");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName() + "执行完毕");
}
}
}
执行结果为:
2024-04-10 20:36:43.649 线程:Thread-0启动
2024-04-10 20:36:43.649 线程:Thread-2启动
2024-04-10 20:36:43.649 线程:Thread-1启动
2024-04-10 20:36:46.650 线程:Thread-2执行完毕
2024-04-10 20:36:48.650 线程:Thread-0执行完毕
2024-04-10 20:36:48.650 线程:Thread-1执行完毕
两种方式的差异及区别:使用集成Thread或者使用实现Runnable方式本质是没有特殊区别,如果在业务场景上有复杂的线程操作需求,那就选择继承Thread,如果只是简单的执行一个任务,那就实现runnable