拓冰建站拓冰建站
首页 / 资讯中心 / 正文

线程四种创建方式

1. 继承Thread类第一步定义Thread类的子类重写该类的run()方法该run()方法的方法体就代表了线程需要完成的任务第二步创建Thread类的子类对象即创建了线程对象第三步调用线程对象的start()方法来创建并启动线程/** * author Evan Walker * version 1.0 * 昂焱数据 https://www.ayshuju.com/ * desc 继承Thread类创建线程 * date 2023/03/24 14:47:12 */ public class MyThread extends Thread { public static void main(String[] args) { System.out.println(当前线程是Thread.currentThread().getName()); new MyThread().start();//创建并启动线程 } Override public void run() { // 业务代码 System.out.println(当前线程是Thread.currentThread().getName()); } }2. 实现Runnable接口第一步定义Runnable接口的实现类重写该接口的run()方法该run()方法的方法体就是线程的线程执行体第二步创建Runnable接口的实现类的此实例作为Thread类的target来创建Thread对象该Thread对象才是真正的线程对象第三步调用线程对象的start()方法来创建并启动线程/** * author Evan Walker * version 1.0 * 昂焱数据 https://www.ayshuju.com/ * desc 实现Runnable接口创建线程 * date 2023/03/24 14:47:12 */ public class MyRunnable implements Runnable { public static void main(String[] args) { System.out.println(当前线程是Thread.currentThread().getName()); Thread thread new Thread(new MyRunnable()); thread.start();//创建并启动线程 } Override public void run() { // 业务代码 System.out.println(当前线程是Thread.currentThread().getName()); } }3. 实现Callable接口第一步创建Callable接口的实现类并实现call()方法。该call()方法将作为线程的执行体旦该call()方法有返回值再创建Callable实现类的实例。第二步使用Future Task类来包装Callable对象该FutureTask对象封装了该Callable对象的call()方法的返回值。第三步使用FutureTask对象作为Thread对象的target创建井启动新线程。第四步使用FutureTask对象的get()方法来获得子线程执行结束后的返回值。import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; /** * author Evan Walker * version 1.0 * 昂焱数据 https://www.ayshuju.com/ * desc 实现Callable接口创建线程 * date 2023/03/24 14:47:12 */ public class MyCallable implements CallableString { public static void main(String[] args) throws Exception { System.out.println(当前线程是 Thread.currentThread().getName()); //创建FutureTask的对象 FutureTaskString task new FutureTaskString(new MyCallable()); //创建Thread类的对象 Thread thread new Thread(task); //开启线程 thread.start(); //获取call()方法的返回值即线程运行结束后的返回值 String result task.get(); System.out.println(result); } Override public String call() throws Exception { // 业务代码 System.out.println(当前线程是 Thread.currentThread().getName()); return call; } }4.线程池创建线程第一步使用Executors类中的方法创建线程池或使用ThreadPoolExecutor方法创建线程池第二步调用线程池中的execute()方法执行由继承Thread类或实现Runnable接口或实现Callable接口创建的线程第三步调用submit()方法执行由实现Callable接口创建的线程第四步调用线程池中的shutdown()方法关闭线程池import java.util.concurrent.*; /** * author Evan Walker * version 1.0 * 昂焱数据 https://www.ayshuju.com/ * desc 线程池创建线程 * date 2023/03/24 14:47:12 */ public class MyExecutor { //起的线程数 private static final Integer THREAD_NUM 5; public static void main(String[] args) throws Exception { Thread.currentThread().setName(主线程); System.out.println(Thread.currentThread().getName():输出的结果); //通过线程池工厂创建线程数量为2的线程池 //创建线程池 ThreadPoolExecutor pool new ThreadPoolExecutor(THREAD_NUM, THREAD_NUM * 2, 0, TimeUnit.SECONDS, new LinkedBlockingQueue(100)); //执行线程,execute()适用于实现Runnable接口创建的线程 pool.execute(new MyThread1()); pool.execute(new MyRunnable1()); //submit()适用于实现Callable接口创建的线程 FutureString task pool.submit(new MyCallable1()); //获取call()方法的返回值 String result task.get(); System.out.println(result); //关闭线程池 pool.shutdown(); } } //继承Thread类 class MyThread1 extends Thread{ Override public void run() { System.out.println(Thread.currentThread().getName():输出的结果); } } //实现Runnable接口 class MyRunnable1 implements Runnable{ Override public void run() { System.out.println(Thread.currentThread().getName():输出的结果); } } //实现Callable接口 class MyCallable1 implements CallableString{ Override public String call(){ System.out.println(Thread.currentThread().getName():输出的结果); return Thread.currentThread().getName():返回的结果; } }
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门