继承Thread类
定义MyThread类->重写run()->创建MyThread对象->调用start()
代码如下:
package com.kingh.thread.create;
/**
* 继承Thread类的方式创建线程
*
* @author <a href="https://blog.csdn.net/king_kgh>Kingh</a>
* @version 1.0
* @date 2019/3/13 19:19
*/
public class CreateThreadDemo1 extends Thread {
public CreateThreadDemo1() {
// 设置当前线程的名字
this.setName("MyThread");
}
@Override
public void run() {
// 每隔1s中输出一次当前线程的名字
while (true) {
// 输出线程的名字,与主线程名称相区分
printThreadInfo();
try {
// 线程休眠一秒
Thread.sleep(1000);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) throws Exception {
// 注意这里,要调用start方法才能启动线程,不能调用run方法
new CreateThreadDemo1().start();
// 演示主线程继续向下执行
while (true) {
printThreadInfo();
Thread.sleep(1000);
}
}
/**
* 输出当前线程的信息
*/
private static void printThreadInfo() {
System.out.println("当前运行的线程名为: " + Thread.currentThread().getName());
}
}
当前运行的线程名为: main
当前运行的线程名为: MyThread
当前运行的线程名为: main
当前运行的线程名为: MyThread
当前运行的线程名为: MyThread
当前运行的线程名为: main
实现Runnable接口
定义MyRunnable实现类->重写run()->创建MyRunnable对象
创建Thread对象->MyRunnable对象作为参数->调用start()
package com.kingh.thread.create;
/**
* 创建线程with lambda
*
* @author <a href="https://blog.csdn.net/king_kgh>Kingh</a>
* @version 1.0
* @date 2019/3/18 10:04
*/
public class CreateThreadDemo5_Lambda {
public static void main(String[] args) throws Exception {
// 使用lambda的形式实例化线程任务类
Runnable task = () -> {
while (true) {
// 输出线程的名字
printThreadInfo();
}
};
// 创建线程对象,并将线程任务类作为构造方法参数传入
new Thread(task).start();
// 主线程的任务,为了演示多个线程一起执行
while (true) {
printThreadInfo();
Thread.sleep(1000);
}
}
/**
* 输出当前线程的信息
*/
private static void printThreadInfo() {
System.out.println("当前运行的线程名为: " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
sub class
sub class
带返回值的线程实现方式
继承Thread类和实现Runnable接口的方式都无法抛出更多的异常,执行完任务也无法得到返回值,Callable接口的call()可以解决这两个问题。
定义MyCallable类->重写call()->创建MyCallable对象
创建FutureTask对象(MyCallable对象作为参数)
创建Thread对象(FutureTask对象作为参数)->调用start()
FutureTask对象调用get()
package com.kingh.thread.create;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
/**
* 带返回值的方式
*
* @author <a href="https://blog.csdn.net/king_kgh>Kingh</a>
* @version 1.0
* @date 2019/3/18 10:04
*/
public class CreateThreadDemo11_Callable {
public static void main(String[] args) throws Exception {
// 创建线程任务
Callable<Integer> call = () -> {
System.out.println("线程任务开始执行了....");
Thread.sleep(2000);
return 1;
};
// 将任务封装为FutureTask
FutureTask<Integer> task = new FutureTask<>(call);
// 开启线程,执行线程任务
new Thread(task).start();
// ====================
// 这里是在线程启动之后,线程结果返回之前
System.out.println("这里可以为所欲为....");
// ====================
// 为所欲为完毕之后,拿到线程的执行结果
Integer result = task.get();
System.out.println("主线程中拿到异步任务执行的结果为:" + result);
}
}
这里可以为所欲为....
线程任务开始执行了....
主线程中拿到异步任务执行的结果为:1
基于线程池的方式
线程是宝贵的资源
创建ExecutorService对象->调用execute(Runnable对象)
package com.kingh.thread.create;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 线程池
*
* @author <a href="https://blog.csdn.net/king_kgh>Kingh</a>
* @version 1.0
* @date 2019/3/18 10:04
*/
public class CreateThreadDemo12_ThreadPool {
public static void main(String[] args) throws Exception {
// 创建固定大小的线程池
ExecutorService threadPool = Executors.newFixedThreadPool(10);
while (true) {
// 提交多个线程任务,并执行
threadPool.execute(new Runnable() {
@Override
public void run() {
printThreadInfo();
}
});
}
}
/**
* 输出当前线程的信息
*/
private static void printThreadInfo() {
System.out.println("当前运行的线程名为: " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
当前运行的线程名为: pool-1-thread-1
当前运行的线程名为: pool-1-thread-2
当前运行的线程名为: pool-1-thread-4
当前运行的线程名为: pool-1-thread-3
当前运行的线程名为: pool-1-thread-7
当前运行的线程名为: pool-1-thread-8
当前运行的线程名为: pool-1-thread-9
当前运行的线程名为: pool-1-thread-6
当前运行的线程名为: pool-1-thread-5
当前运行的线程名为: pool-1-thread-10