Java多线程的几种实现方式

Java多线程的几种实现方式,第1张

Java多线程的几种实现方式

在工作当中,个人常用多线程来做一些数据处理的工作,以此来加快数据处理速度。截止到Java8,实现多线程有4种写法,记下相关代码备查。

通过继承Tread类开启多线程
public static void main(String[] args) {
    System.out.println("主线程名称:" + Thread.currentThread().getName());
    Task task1 = new Task();
    Task task2 = new Task();
    task1.start();
    task2.start();
}

public static class Task extends Thread {

    @Override
    public void run() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println("线程" + Thread.currentThread().getName() + "的消息:" + i);
                Thread.sleep(800);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
复制代码
通过Runnable接口实现多线程
public static void main(String[] args) {
    System.out.println("主线程名称:" + Thread.currentThread().getName());
    Thread task1 = new Thread(new Task());
    Thread task2 = new Thread(new Task());
    task1.start();
    task2.start();
}

public static class Task implements Runnable {

    @Override
    public void run() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println("线程" + Thread.currentThread().getName() + "的消息:" + i);
                Thread.sleep(800);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
复制代码
通过Callable+FutureTask实现带返回值的多线程
public static void main(String[] args) {
    System.out.println("主线名称:" + Thread.currentThread().getName());
    Callable crb1 = new Task();
    Callable crb2 = new Task();
    FutureTask task1 = new FutureTask<>(crb1);
    FutureTask task2 = new FutureTask<>(crb2);
    Thread thread1 = new Thread(task1);
    Thread thread2 = new Thread(task2);
    thread1.start();
    thread2.start();
    try {
        System.out.println("task1返回结果:" + task1.get());
        System.out.println("task2返回结果:" + task2.get());
    } catch (Exception e) {
        System.out.println("获取FutureTask返回结果异常:" + e.getMessage());
    }
}

public static class Task implements Callable {

    @Override
    public String call() throws Exception {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println("线程" + Thread.currentThread().getName() + "的消息:" + i);
                Thread.sleep(800);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        return "返回结果:" + Thread.currentThread().getName();
    }
}
复制代码
通过线程池技术实现多线程(推荐)

Executors

public static void main(String[] args) {
    System.out.println("主线程名称:" + Thread.currentThread().getName());
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    executorService.execute(new Task());
    executorService.execute(new Task());
}

public static class Task implements Runnable {

    @Override
    public void run() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println("线程" + Thread.currentThread().getName() + "的消息:" + i);
                Thread.sleep(800);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
复制代码

ThreadPoolExecutor

public static void main(String[] args) {
    System.out.println("主线程名称:" + Thread.currentThread().getName());
    ThreadPoolExecutor executorService = new ThreadPoolExecutor(2, 2, 5, TimeUnit.MINUTES, new linkedBlockingDeque());
    executorService.execute(new Task());
    executorService.execute(new Task());
}

public static class Task implements Runnable {

    @Override
    public void run() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println("线程" + Thread.currentThread().getName() + "的消息:" + i);
                Thread.sleep(800);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}


作者:独木不成林
链接:https://juejin.cn/post/7048074430961942564
 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5693094.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存