一、重写 Thread 类的 run() 方法。
表现形式有两种:1)new Thread 对象匿名重写 run() 方法
packageconstxiong.concurrency.a006;/*** new Thread 对象匿名重写 run() 方法,启动线程
*@authorConstXiong*/
public classTestNewThread {public static voidmain(String[] args) {//创建线程 t, 重写 run() 方法
new Thread("t") {
@Overridepublic voidrun() {for (int i = 0; i < 3; i++) {
System.out.println("thread t > " +i);
}
}
}.start();
}
}
执行结果
thread t > 0
thread t > 1
thread t > 2
2)继承 Thread 对象,重写 run() 方法
packageconstxiong.concurrency.a006;/*** 继承 Thread 类,重写 run() 方法
*@authorConstXiong*/
public classTestExtendsThread {public static voidmain(String[] args) {newThreadExt().start();
}
}//ThreadExt 继承 Thread,重写 run() 方法
class ThreadExt extendsThread {
@Overridepublic voidrun() {for (int i = 0; i < 3; i++) {
System.out.println("thread t > " +i);
}
}
}
执行结果
thread t > 0
thread t > 1
thread t > 2
二、实现 Runnable 接口,重写 run() 方法。
表现形式有两种:1)new Runnable 对象,匿名重写 run() 方法
packageconstxiong.concurrency.a006;/*** new Runnalbe 对象匿名重写 run() 方法,启动线程
*@authorConstXiong*/
public classTestNewRunnable {public static voidmain(String[] args) {
newRunnable();
}public static voidnewRunnable() {//创建线程 t1, 重写 run() 方法
new Thread(newRunnable() {
@Overridepublic voidrun() {for (int i = 0; i < 3; i++) {
System.out.println("thread t1 > " +i);try{
Thread.sleep(1000L);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
},"t1").start();//创建线程 t2, lambda 表达式设置线程的执行代码//JDK 1.8 开始支持 lambda 表达式
new Thread(() ->{for (int i = 0; i < 3; i++) {
System.out.println("thread t2 > " +i);try{
Thread.sleep(1000L);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
},"t2").start();
}
}
执行结果
thread t1 > 0
thread t2 > 0
thread t1 > 1
thread t2 > 1
thread t1 > 2
thread t2 > 2
2)实现 Runnable 接口,重写 run() 方法
packageconstxiong.concurrency.a006;/*** 实现 Runnable 接口,重写 run() 方法
*@authorConstXiong*/
public classTestImplRunnable {public static voidmain(String[] args) {new Thread(newRunnableImpl()).start();
}
}///RunnableImpl 实现 Runnalbe 接口,重写 run() 方法
class RunnableImpl implementsRunnable {
@Overridepublic voidrun() {for (int i = 0; i < 3; i++) {
System.out.println("thread t > " +i);
}
}
}
执行结果
thread t > 0
thread t > 1
thread t > 2
三、实现 Callable 接口,使用 FutureTask 类创建线程
packageconstxiong.concurrency.a006;importjava.util.concurrent.Callable;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.FutureTask;/*** 实现 Callable 接口,使用 FutureTask 类创建线程
*@authorConstXiong*/
public classTestCreateThreadByFutureTask {public static void main(String[] args) throwsInterruptedException, ExecutionException {//通过构造 FutureTask(Callable callable) 构造函数,创建 FutureTask,匿名实现接口 Callable 接口
FutureTask ft = new FutureTask(new Callable() {
@Overridepublic String call() throwsException {return "ConstXiong";
}
});//Lambda 方式实现//FutureTask ft = new FutureTask(() -> "ConstXiong");
newThread(ft).start();
System.out.println("执行结果:" +ft.get());
}
}
执行结果
执行结果:ConstXiong
四、使用线程池创建、启动线程
packageconstxiong.concurrency.a006;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;/*** 线程池的方式启动线程
*@authorConstXiong*/
public classTestCreateThreadByThreadPool {public static voidmain(String[] args) {//使用工具类 Executors 创建单线程线程池
ExecutorService singleThreadExecutor =Executors.newSingleThreadExecutor();//提交执行任务
singleThreadExecutor.submit(() -> {System.out.println("单线程线程池执行任务");});//关闭线程池
singleThreadExecutor.shutdown();
}
}
执行结果
单线程线程池执行任务
PS:这边只是简单的使用示例,至于 Runnable 接口和 Callable 接口的区别、线程池的使用,后面详细探索。