Java实现多线程的三种方式:继承Thread类;实现Runnable接口;实现Callable接口

实现线程的众所周知的方法具体有2种,但是还有一种,估计不是人人都知道的,没搞过多线程编程的,估计就不知道啦:

(1)继承Thread类,重写run方法

(2)实现Runnable接口,重写run方法

(3)实现Callable接口,重写call方法

一直天真的以为只有两种方式来实现多线程,直到我真的来仔细琢磨学习了一下这个问题,才知道,Java后来优化了问题。新增了个高级方式。


然后我做如下具体实例来测试。

第一种实现方式:实现Runnable接口,重写run方法

[java]  view plain  copy
  1. package com.lxk.threadTest.wayToThread;  
  2.   
  3. /** 
  4.  * Created by lxk on 2017/6/25 
  5.  * 创建线程方式1实现 
  6.  * 实现Runnable接口,重写run方法 
  7.  */  
  8. public class ThreadWay1 implements Runnable {  
  9.     @Override  
  10.     public void run() {  
  11.         for (; ; ) {  
  12.             try {  
  13.                 Thread.sleep(1000);//毫秒  
  14.                 System.out.println(Thread.currentThread().getName() + "...implements Runnable way");  
  15.             } catch (InterruptedException e) {  
  16.                 e.printStackTrace();  
  17.             }  
  18.         }  
  19.     }  
  20. }  


第二种实现方式:继承Thread类,重写run方法

[java]  view plain  copy
  1. package com.lxk.threadTest.wayToThread;  
  2.   
  3. /** 
  4.  * Created by lxk on 2017/6/25 
  5.  * 创建线程方式2实现 
  6.  * 继承Thread类,重写run函数 
  7.  */  
  8. public class ThreadWay2 extends Thread {  
  9.     @Override  
  10.     public void run() {  
  11.         for (; ; ) {  
  12.             try {  
  13.                 Thread.sleep(1000);//毫秒  
  14.                 System.out.println(Thread.currentThread().getName() + "...extends way");  
  15.             } catch (InterruptedException e) {  
  16.                 e.printStackTrace();  
  17.             }  
  18.         }  
  19.     }  
  20. }  

第三种实现方式:实现Callable接口,重写call方法

[java]  view plain  copy
  1. package com.lxk.threadTest.wayToThread;  
  2.   
  3. import java.util.concurrent.Callable;  
  4.   
  5. /** 
  6.  * Callable和Runnable有几点不同: 
  7.  * (1)Callable规定的方法是call(),而Runnable规定的方法是run(). 
  8.  * (2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。 
  9.  * (3)call()方法可抛出异常,而run()方法是不能抛出异常的。 
  10.  * (4)运行Callable任务可拿到一个Future对象, 
  11.  * <p> 
  12.  * Created by lxk on 2017/6/27 
  13.  */  
  14. public class ThreadWay3 implements Callable<Integer> {  
  15.   
  16.     @Override  
  17.     public Integer call() throws Exception {  
  18.         int sum = 0;  
  19.         for (int i = 0; i < 10; i++) {  
  20.             try {  
  21.                 Thread.sleep(1000);//毫秒  
  22.                 sum += 1;  
  23.                 System.out.println(Thread.currentThread().getName() + "...implements Callable<T>..." + sum);  
  24.             } catch (InterruptedException e) {  
  25.                 e.printStackTrace();  
  26.             }  
  27.         }  
  28.         return sum;  
  29.     }  
  30. }  
只是简单的记录下,如何实现,后面有机会再总结一下这个,我真不常见的这个实现方式。


然后就是在main方法里面调用,运行一下的代码。

[java]  view plain  copy
  1. package com.lxk.threadTest.wayToThread;  
  2.   
  3. import java.util.concurrent.FutureTask;  
  4.   
  5. /** 
  6.  * 线程测试(实现多线程的几种方式) 
  7.  * <p> 
  8.  * Created by lxk on 2016/11/12 
  9.  */  
  10. public class Main {  
  11.     public static void main(String[] args) {  
  12.         newThreadWay1();//实现Runnable接口  
  13.         //newThreadWay2();//继承Thread类  
  14.         //newThreadWay3();//实现Callable接口  
  15.         //newThreadWay1_();//也就是个简单的匿名对象,直接实现Runnable接口,省去了新建个类的步骤  
  16.         //newThreadWay1__();//一样也是实现Runnable接口,省去了新建个类的步骤,不过不是匿名对象而已  
  17.     }  
  18.   
  19.     /** 
  20.      * 创建线程方式1实现 
  21.      * 实现Runnable接口,重写run方法 
  22.      * 实现接口的优点:可以方便扩展 
  23.      */  
  24.     private static void newThreadWay1() {  
  25.         ThreadWay1 threadWay1 = new ThreadWay1();  
  26.         new Thread(threadWay1).start();  
  27.         //new Thread(new ThreadWay1()).start();//等于上面的2行代码  
  28.         System.out.println(Thread.currentThread().getName() + "...implements Runnable way");  
  29.     }  
  30.   
  31.   
  32.     /** 
  33.      * 创建线程方式2实现 
  34.      * 继承Thread类,重写run函数 
  35.      */  
  36.     private static void newThreadWay2() {  
  37.         ThreadWay2 threadWay2 = new ThreadWay2();  
  38.         threadWay2.start();  
  39.         //new ThreadWay2().start();//等于上面的2行代码  
  40.         System.out.println(Thread.currentThread().getName() + "...extends way");  
  41.     }  
  42.   
  43.     /** 
  44.      * 创建线程方式3实现 
  45.      * 实现Callable接口,重写call方法 
  46.      */  
  47.     private static void newThreadWay3() {  
  48.         int resultFromThread;  
  49.         try {  
  50.             ThreadWay3 threadWay3 = new ThreadWay3();  
  51.             FutureTask<Integer> future = new FutureTask<>(threadWay3);  
  52.             new Thread(future).start();  
  53.             Thread.sleep(5000);// 可能做一些事情  
  54.             //注意:  
  55.             // 这个有个问题:主线程必须等子线程执行完,才可以继续执行,变成了同步啦,这就不太好啦。失去了多线程的意义啦。  
  56.             resultFromThread = future.get();  
  57.             System.out.println(Thread.currentThread().getName() + "...implements Callable<T>..." + resultFromThread);  
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  
  62.   
  63.     /** 
  64.      * 创建线程方式1实现 
  65.      * 实现Runnable接口,重写run方法 
  66.      * (匿名对象:new个线程对象直接开启) 
  67.      */  
  68.     private static void newThreadWay1_() {  
  69.         new Thread(new Runnable() {  
  70.             @Override  
  71.             public void run() {  
  72.                 for (; ; ) {//死循环  
  73.                     try {  
  74.                         Thread.sleep(1000);//毫秒  
  75.                         System.out.println(Thread.currentThread().getName() + "...new Runnable()匿名对象");  
  76.                     } catch (InterruptedException e) {  
  77.                         e.printStackTrace();  
  78.                     }  
  79.                 }  
  80.   
  81.             }  
  82.         }).start();  
  83.         System.out.println(Thread.currentThread().getName() + "......new Runnable()匿名对象");  
  84.     }  
  85.   
  86.     /** 
  87.      * 创建线程方式1实现 
  88.      * 实现Runnable接口,重写run方法 
  89.      * (实例化一个线程对象,然后调用start方法开启线程) 
  90.      */  
  91.     private static void newThreadWay1__() {  
  92.         Thread thread = new Thread(new Runnable() {  
  93.             @Override  
  94.             public void run() {  
  95.                 for (; ; ) {  
  96.                     try {  
  97.                         Thread.sleep(1000);//毫秒  
  98.                         System.out.println(Thread.currentThread().getName() + "...new Runnable()非匿名对象");  
  99.                     } catch (InterruptedException e) {  
  100.                         e.printStackTrace();  
  101.                     }  
  102.                 }  
  103.             }  
  104.         });  
  105.         thread.start();  
  106.         System.out.println(Thread.currentThread().getName() + "...new Runnable()非匿名对象");  
  107.     }  
  108. }  


然后就是各个实现方式 的代码运行结果图 ,如下:

可以看到,起码是看到2个线程在跑,而且,callable接口的main线程是在后面的,是有点特殊的。

[java]  view plain  copy
  1. 一下摘译Java 1.7 jdk的Thread类里面的一段注释  
  2.  public class Thread implements Runnable {......}  
  3.   
  4.  * There are two ways to create a new thread of execution. One is to  
  5.  * declare a class to be a subclass of <code>Thread</code>. This  
  6.  * subclass should override the <code>run</code> method of class  
  7.  * <code>Thread</code>. An instance of the subclass can then be  
  8.  * allocated and started.   
  9.    
  10.  //。。。。。。省略他自带的例子,就是继承Thread类,重写run();  
  11.    
  12.  * The other way to create a thread is to declare a class that  
  13.  * implements the <code>Runnable</code> interface. That class then  
  14.  * implements the <code>run</code> method. An instance of the class can  
  15.  * then be allocated, passed as an argument when creating  
  16.  * <code>Thread</code>, and started.   
  17.  //。。。。。。省略他自带的例子,就是实现Runnable接口,还是重写run();  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值