Java多线程的使用有三种方法总结

Java多线程的使用有三种方法总结:

继承Thread类

实现Runnable接口

和使用Callable和Future创建线程

本文将对这三种方法一一进行介绍。

1、继承Thread类


public class MyThread extends Thread {
    @Override
    public void run(){
        super.run();
        System.out.println("执行子线程...");
    }
}
public class Test {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
        System.out.println("主线程...");
    }
}

2、实现Runnable接口


public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("执行子线程...");
    }
}
public class Test {
    public static void main(String[] args) {

        Runnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
        System.out.println("主线程运行结束!");
    }
}

3、使用Callable和Future创建线程

import java.util.concurrent.Callable;

public class MyCallable implements Callable {
    int i = 0;
    @Override
    public Object call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"  i的值:"+ i);
        return i++; //call方法可以有返回值
    }
}
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class Test {
    public static void main(String[] args) {
        Callable callable = new MyCallable();
        for (int i = 0; i < 10; i++) {
            FutureTask task = new FutureTask(callable);
            new Thread(task,"子线程"+ i).start();
            try {
                //获取子线程的返回值
                System.out.println("子线程返回值:"+task.get() + "\n");
            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

5、三种方式的对比

第一种和后面两种的对比:

1、通过代码可以看出,第一种方法是最简洁方便的,直接就可以start,不需要任何转换
2、但是第一种有一个很不好的地方就是继承了Thread类后由于java的单继承机制,就不可以继承其他的类了,而如果实现的是接口,就可以实现多个接口,使开发更灵活。

第二种和第三种方式对比:

1、同样的,第二种方法相对第三种方式来说代码更简洁,使用更方便,少了一次转换
2、第三种方法有两个优点:有返回值等待执行结果、可以抛出异常

总结:

第一种和第二种,都是对run方法进行重写或实现,并且都是线程异步

第三种实现call方法,并且子线程异步主线程同步等待结果集

补充

在java1.8中使用Lambda可以轻松实现一个线程

new Thread(()-> 
       EmailUtil.sendWorkbookGropBuy(gropBuyService.creatGropBuy(businessId,cardId ,userId         
       ,count),user.getEmail())
).start();
发布了24 篇原创文章 · 获赞 0 · 访问量 404

猜你喜欢

转载自blog.csdn.net/WillliveWillWork/article/details/101367962
今日推荐