zoukankan      html  css  js  c++  java
  • Java中Runnable和Thread以及Callable的区别

    http://blog.csdn.net/huang211630/article/details/45640071


            在java中,其实java中实现 多线程有三种方法,一种是继承Thread类;第二种是实现Runnable接口;第三种是实现Callable接口。

    1,继承Thread

            Thread类是在java.lang包中定义的。一个类只要继承了Thread类同时覆写了本类中的run()方法就可以实现多线程操作了,但是一个类只能继承一个父类,这是此方法的局限。

            下面看例子:

    package org.thread.demo;
      class MyThread extends Thread{
      private String name;
      public MyThread(String name) {
      super();
      this.name = name;
      }
      public void run(){
      for(int i=0;i<10;i++){
      System.out.println("线程开始:"+this.name+",i="+i);
      }
      }
      }
      package org.thread.demo;
      public class ThreadDemo01 {
      public static void main(String[] args) {
      MyThread mt1=new MyThread("线程a");
      MyThread mt2=new MyThread("线程b");
      mt1.run();
      mt2.run();
      }
      }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

            但是,此时结果很有规律,先第一个对象执行,然后第二个对象执行,并没有相互运行。在JDK的文档中可以发现,一旦调用start()方法,则会通过JVM找到run()方法。下面启动start()方法启动线程:

    package org.thread.demo;  
    public class ThreadDemo01 {  
    public static void main(String[] args) {  
    MyThread mt1=new MyThread("线程a");  
    MyThread mt2=new MyThread("线程b");  
    mt1.start();  
    mt2.start();  
    }  
    }; 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

            这样程序可以正常完成交互式运行。那么为啥非要使用start();方法启动多线程呢?

            在JDK的安装路径下,src.zip是全部的java源程序,通过此代码找到Thread中的start()方法的定义,可以发现此方法中使用了private native void start0();其中native关键字表示可以调用操作系统的底层函数,那么这样的技术成为JNI技术(java Native Interface)

    2,Runnable接口

            在实际开发中一个多线程的操作很少使用Thread类,而是通过Runnable接口

    public interface Runnable{  
    public void run();  
    } 
    例子:
    package org.runnable.demo;  
    class MyThread implements Runnable{  
    private String name;  
    public MyThread(String name) {  
    this.name = name;  
    }
    public void run(){  
    for(int i=0;i<100;i++){  
    System.out.println("线程开始:"+this.name+",i="+i);  
    }  
    }  
    }; 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

            使用Runnable定义的子类中没有start()方法,只有Thread类中才有。此时观察Thread类,有一个构造方法:public Thread(Runnable targer)此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多线程。(start()可以协调系统的资源):

    package org.runnable.demo;  
    import org.runnable.demo.MyThread;  
    public class ThreadDemo01 {  
    public static void main(String[] args) {  
    MyThread mt1=new MyThread("线程a");  
    MyThread mt2=new MyThread("线程b");  
    new Thread(mt1).start();  
    new Thread(mt2).start();  
    }  
    } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

            两种实现方式的区别和联系:

            在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下好处:

                    1,避免点继承的局限,一个类可以继承多个接口。 
                    2,适合于资源的共享

            Runnable接口和Thread之间的联系:

            public class Thread extends Object implements Runnable 
            发现Thread类也是Runnable接口的子类。

    3,Callable接口

            Callable 和 Runnable 的使用方法大同小异, 区别在于: 
                    1.Callable 使用 call() 方法, Runnable 使用 run() 方法 
                    2.call() 可以返回值, 而 run()方法不能返回。 

                    3.call() 可以抛出受检查的异常,比如ClassNotFoundException, 而run()不能抛出受检查的异常。

    个人认为:确切的说,callalbe机制可以向上抛出异常,而runnable机制不能

            Callable示例如下:

    Java代码 
    class TaskWithResult implements Callable<String> {  
        private int id;  
    
        public TaskWithResult(int id) {  
            this.id = id;  
        }  
    
        @Override  
        public String call() throws Exception {  
            return "result of TaskWithResult " + id;  
        }  
    }  
    
    public class CallableTest {  
        public static void main(String[] args) throws InterruptedException,  
                ExecutionException {  
            ExecutorService exec = Executors.newCachedThreadPool();  
            ArrayList<Future<String>> results = new ArrayList<Future<String>>();    //Future 相当于是用来存放Executor执行的结果的一种容器  
            for (int i = 0; i < 10; i++) {  
                results.add(exec.submit(new TaskWithResult(i)));  
            }  
            for (Future<String> fs : results) {  
                if (fs.isDone()) {  
                    System.out.println(fs.get());  
                } else {  
                    System.out.println("Future result is not yet complete");  
                }  
            }  
            exec.shutdown();  
        }  
    }  

  • 相关阅读:
    linux安装java环境
    win7+ubuntu双系统安装攻略
    Ubuntu 安装基础教程
    我学到的算法
    linux使用su切换用户提示 Authentication failure的解决方法& 复制文件时,报cp: omitting directory `XXX'
    我新买的红米手机,新浪和360浏览器都能进,也能看电视,就是不能上手机QQ和微信
    电脑如何设置自动开机
    jsp分页技术
    发光文本框样式
    子元素div高度不确定时父div高度如何自适应
  • 原文地址:https://www.cnblogs.com/silyvin/p/9106755.html
Copyright © 2011-2022 走看看