• 使用Callable和Future创建线程


    Java创建线程主要有三种方式:

      1、继承Thread类

      2、实现Runnable接口

      3、使用Callable和Future创建线程

        参考地址:https://www.cnblogs.com/yeya/p/10183366.html

    一、继承Thread类

    步骤:

      1、创建一个线程子类继承Thread类

      2、重写run() 方法,把需要线程执行的程序放入run方法,线程启动后方法里的程序就会运行

      2、创建该类的实例,并调用对象的start()方法启动线程

    示例代码如下:

     1 public class ThreadDemo extends Thread{
     2     @Override
     3     public void run() {
     4         super.run();
     5         System.out.println("需要运行的程序。。。。。。。。");
     6     }
     7 
     8     public static void main(String[] args) {
     9         Thread thread = new ThreadDemo();
    10         thread.start();
    11     }
    12 }

    二、实现Runnable接口

      1、定义一个线程类实现Runnable接口,并重写该接口的run()方法,方法中依然是包含指定执行的程序。

      2、创建一个Runnable实现类实例,将其作为target参数传入,并创建Thread类实例。

      3、调用Thread类实例的start()方法启动线程。

    示例代码如下:

     1 public class RunnableDemo implements Runnable{
     2     @Override
     3     public void run() {
     4         System.out.println("我是Runnable接口......");
     5     }
     6     public static void main(String[] args) {
     7 
     8         RunnableDemo demo = new RunnableDemo();
     9         Thread thread = new Thread(demo);
    10         thread.start();
    11     }
    12 }

    三、使用Callable和Future创建线程

    使用Callable创建线程和Runnable接口方式创建线程比较相似,不同的是,Callable接口提供了一个call() 方法作为线程执行体,而Runnable接口提供的是run()方法,同时,call()方法可以有返回值,而且需要用FutureTask类来包装Callable对象。

    1 public interface Callable<V> {
    2     V call() throws Exception;
    3 }

    步骤:

      1、创建Callable接口的实现类,实现call() 方法

      2、创建Callable实现类实例,通过FutureTask类来包装Callable对象,
         该对象封装了Callable对象的call()方法的返回值。

      3、将创建的FutureTask对象作为target参数传入,创建Thread线程实例并启动新线程。

      4、调用FutureTask对象的get方法获取返回值。

    示例代码如下:

    ①.创建实现Callable接口的类

     1 /**
     2  *<p> Description: 使用Callable与Future创建可会获取返回值的线程 </p>
     3  *<p> Copyright: Copyright(c) 2018/12/27 </p>
     4  *<p> Company: xxx </p>
     5  *
     6  *@author Jason
     7  *@Version 1.0 2018/12/27 14:15
     8  */
     9 public class ThreadUtil implements Callable {
    10 
    11     private StudentService studentService;
    12 
    13     public ThreadUtil() {
    14     }
    15 
    16     public ThreadUtil(StudentService studentService) {
    17         this.studentService = studentService;
    18     }
    19 
    20     /**
    21      * 重写call()方法,查询数据库
    22      * @return List<Student>
    23      * @throws Exception 异常
    24      */
    25     @Override
    26     public List<Student> call() throws Exception {
    27         List<Student> students = studentService.allStudentsList();
    28         return students;
    29     }
    30 }

    ②.测试开启线程获取线程返回的数据

     1 /**
     2  *<p> Description: 线程测试类 </p>
     3  *<p> Copyright: Copyright(c) 2018/12/27 </p>
     4  *<p> Company: xxx </p>
     5  *
     6  *@author Jason
     7  *@Version 1.0 2018/12/27 14:22
     8  */
     9 @RunWith(SpringJUnit4ClassRunner.class)
    10 @SpringBootTest
    11 public class ThreadTest
    12 {
    13     
    14     @Autowired
    15     private StudentService studentService;
    16     
    17     /**
    18      * 开启线程查询数据库
    19      */
    20     @Test
    21     public void testThread1()
    22     {
    23         // 1.获取FutureTask对象
    24         ThreadUtil threadUtil = new ThreadUtil(studentService);
    25         FutureTask futureTask = new FutureTask(threadUtil);
    26         
    27         // 2.开启线程
    28         new Thread(futureTask).start();
    29         
    30         try
    31         {
    32            // 3.使用Futura#get()方法获取线程的返回值
    33             List<Student> studentList = (List<Student>) futureTask.get();
    34             studentList.forEach(student -> System.out.println(student));
    35         }
    36         catch (InterruptedException e)
    37         {
    38             e.printStackTrace();
    39         }
    40         catch (ExecutionException e)
    41         {
    42             e.printStackTrace();
    43         }
    44     }
    45 }
  • 相关阅读:
    Android总结篇系列:Android Service
    Android 6.0 扫描不到 Ble 设备需开启位置权限
    玩转Android之Drawable的使用
    玩转Android之二维码生成与识别
    玩转Android之数据库框架ActiveAndroid的使用
    玩转Android之加速度传感器的使用,模仿微信摇一摇
    玩转Android之在线视频播放控件Vitamio的使用
    Android5.0之TextInputLayout、FloatingActionButton的使用
    Android自定义View之ProgressBar出场记
    Android5.0之Activity的转场动画
  • 原文地址:https://www.cnblogs.com/jason2018524/p/10184927.html
Copyright © 2020-2023  润新知