zoukankan      html  css  js  c++  java
  • 浅谈多线程的创建方法

    一、多线程的创建方法
        多线程的创建方法有三种:
              1、继承Thread类,重写run()方法
             class  NewThread extends Thread{
                    void run(){}
             }
             class Solution{
                       public static void main(String[] args){
                                 NewThraed td = new NewThread();
                                  td.start();
                       }
             }
             2、实现Runnable接口
             class  NewThread implement Runnable{
                    void run(){}
             }
             class Solution{
                       public static void main(String[] args){
                                  NewThraed ntd = new NewThread();
                                 Thraed td= new Thread(ntd);
                                  td.start();
                       }
             }
             3、实现CallAble接口
              class  NewThread implement Callable{
                    void call(){}
             }
             class Solution{
                       public static void main(String[] args){
                                  NewThraed ntd = new NewThread();
                                 FutureTask ft = new FutureTask(ntd)
                                 Thread td  = new Thread(ft);
                                  td.start();
                       }
             }
    二、CallAble和RunnAble的异同
             相同点:CallAble和RunnAble都是函数式接口(FuncationalInterfice)
             不同点:CallAble实现的多线程有返回值,RunnAble实现的多线程无返回值
    三、CallAble实现多线程浅析               
             CallAble在实现多线程时,需要用CallAble实现类对象作为参数创建FutureTask的对象,再使用FutureTask的对象作为参数创建Thread对           象,完成多线程的创建。最后,FutureTask的对象通过get()方法获取返回值。
             在创建多线程时,并没有以Futuretask的对象作为参数的Thread构造方法,不过查看FutureTask可以知道,FutureTask实现了RunnAbleFuture接口,而RunnAbleFuture接口继承了RunnAble接口和Future接口,所以,FutureTask类具有Runnable接口实现类的所有性质,所有可以使用Thread的构造方法创建多线程。
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    C语言 va_start 宏
    C语言 strcat_s 函数
    C语言 strcat 函数
    C语言 memcpy_s 函数
    C语言 memcpy 函数
    C语言 strcpy_s 函数
    C语言 strcpy 函数
    C语言 sizeof 函数
    c++实现扫雷游戏 初学
    .Net vs .Net Core,我该如何选择?看这一篇文章就够了
  • 原文地址:https://www.cnblogs.com/0515-xbblog/p/15450029.html
Copyright © 2011-2022 走看看