Java中线程的创建方式是什么

发布时间:2023-03-06 10:58:39 作者:iii
来源:亿速云 阅读:156

Java中线程的创建方式是什么

在Java中,线程是程序执行的最小单元,多线程编程是现代软件开发中不可或缺的一部分。Java提供了多种创建线程的方式,每种方式都有其特定的应用场景和优缺点。本文将详细介绍Java中线程的创建方式,包括继承Thread类、实现Runnable接口、使用CallableFuture、以及通过线程池创建线程等。

1. 继承Thread

1.1 基本概念

Thread类是Java中用于表示线程的类。通过继承Thread类并重写其run()方法,可以创建一个新的线程。run()方法中定义了线程执行的任务。

1.2 示例代码

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("线程正在运行: " + Thread.currentThread().getName());
    }
}

public class ThreadExample {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start(); // 启动线程
    }
}

1.3 优缺点

2. 实现Runnable接口

2.1 基本概念

Runnable接口是Java中用于表示可运行任务的接口。通过实现Runnable接口并重写其run()方法,可以将任务封装为一个对象,然后将其传递给Thread类的构造函数来创建线程。

2.2 示例代码

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("线程正在运行: " + Thread.currentThread().getName());
    }
}

public class RunnableExample {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start(); // 启动线程
    }
}

2.3 优缺点

3. 使用CallableFuture

3.1 基本概念

Callable接口与Runnable接口类似,但它可以返回一个结果并抛出异常。Future接口表示异步计算的结果,可以通过Future对象获取Callable任务的返回值。

3.2 示例代码

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
        return "线程返回结果: " + Thread.currentThread().getName();
    }
}

public class CallableExample {
    public static void main(String[] args) {
        MyCallable myCallable = new MyCallable();
        FutureTask<String> futureTask = new FutureTask<>(myCallable);
        Thread thread = new Thread(futureTask);
        thread.start(); // 启动线程

        try {
            String result = futureTask.get(); // 获取线程返回结果
            System.out.println(result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}

3.3 优缺点

4. 使用线程池

4.1 基本概念

线程池是一种管理线程的机制,通过线程池可以有效地控制线程的数量和执行任务的方式。Java提供了ExecutorService接口及其实现类来创建和管理线程池。

4.2 示例代码

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("线程正在运行: " + Thread.currentThread().getName());
    }
}

public class ThreadPoolExample {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5); // 创建固定大小的线程池

        for (int i = 0; i < 10; i++) {
            MyRunnable myRunnable = new MyRunnable();
            executorService.execute(myRunnable); // 提交任务给线程池
        }

        executorService.shutdown(); // 关闭线程池
    }
}

4.3 优缺点

5. 使用CompletableFuture

5.1 基本概念

CompletableFuture是Java 8引入的一个类,用于处理异步编程。它提供了丰富的API来处理异步任务的结果,支持链式调用和组合多个异步任务。

5.2 示例代码

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            return "线程返回结果: " + Thread.currentThread().getName();
        });

        try {
            String result = future.get(); // 获取线程返回结果
            System.out.println(result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}

5.3 优缺点

6. 使用ForkJoinPool

6.1 基本概念

ForkJoinPool是Java 7引入的一个线程池实现,专门用于处理分治任务。它通过工作窃取算法来提高多核CPU的利用率,适合处理递归任务。

6.2 示例代码

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

class MyTask extends RecursiveTask<Integer> {
    private final int start;
    private final int end;

    public MyTask(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Integer compute() {
        if (end - start <= 10) {
            int sum = 0;
            for (int i = start; i <= end; i++) {
                sum += i;
            }
            return sum;
        } else {
            int mid = (start + end) / 2;
            MyTask leftTask = new MyTask(start, mid);
            MyTask rightTask = new MyTask(mid + 1, end);
            leftTask.fork();
            rightTask.fork();
            return leftTask.join() + rightTask.join();
        }
    }
}

public class ForkJoinPoolExample {
    public static void main(String[] args) {
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        MyTask task = new MyTask(1, 100);
        int result = forkJoinPool.invoke(task);
        System.out.println("计算结果: " + result);
    }
}

6.3 优缺点

7. 使用ThreadLocal

7.1 基本概念

ThreadLocal是Java中用于实现线程局部变量的类。每个线程都有自己独立的ThreadLocal变量副本,线程之间互不干扰。

7.2 示例代码

public class ThreadLocalExample {
    private static final ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);

    public static void main(String[] args) {
        Runnable task = () -> {
            int value = threadLocal.get();
            threadLocal.set(value + 1);
            System.out.println("线程 " + Thread.currentThread().getName() + " 的局部变量值: " + threadLocal.get());
        };

        Thread thread1 = new Thread(task);
        Thread thread2 = new Thread(task);

        thread1.start();
        thread2.start();
    }
}

7.3 优缺点

8. 使用TimerTimerTask

8.1 基本概念

TimerTimerTask是Java中用于实现定时任务的类。Timer用于调度任务,TimerTask用于定义任务。

8.2 示例代码

import java.util.Timer;
import java.util.TimerTask;

public class TimerExample {
    public static void main(String[] args) {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("定时任务执行: " + Thread.currentThread().getName());
            }
        };

        timer.schedule(task, 1000, 2000); // 延迟1秒后执行,每隔2秒执行一次
    }
}

8.3 优缺点

9. 使用ScheduledExecutorService

9.1 基本概念

ScheduledExecutorService是Java中用于实现定时任务的接口,它是ExecutorService的子接口,提供了更灵活的定时任务调度功能。

9.2 示例代码

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorServiceExample {
    public static void main(String[] args) {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        Runnable task = () -> System.out.println("定时任务执行: " + Thread.currentThread().getName());

        scheduler.scheduleAtFixedRate(task, 1, 2, TimeUnit.SECONDS); // 延迟1秒后执行,每隔2秒执行一次
    }
}

9.3 优缺点

10. 使用ReentrantLockCondition

10.1 基本概念

ReentrantLock是Java中用于实现可重入锁的类,Condition是用于实现线程间通信的接口。通过ReentrantLockCondition可以实现更灵活的线程同步机制。

10.2 示例代码

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockExample {
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();
    private boolean flag = false;

    public void await() throws InterruptedException {
        lock.lock();
        try {
            while (!flag) {
                condition.await();
            }
            System.out.println("条件满足,继续执行");
        } finally {
            lock.unlock();
        }
    }

    public void signal() {
        lock.lock();
        try {
            flag = true;
            condition.signal();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockExample example = new ReentrantLockExample();

        Thread thread1 = new Thread(() -> {
            try {
                example.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                Thread.sleep(1000);
                example.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();
    }
}

10.3 优缺点

11. 使用CountDownLatch

11.1 基本概念

CountDownLatch是Java中用于实现线程同步的工具类,它允许一个或多个线程等待其他线程完成操作后再继续执行。

11.2 示例代码

import java.util.concurrent.CountDownLatch;

public class CountDownLatchExample {
    public static void main(String[] args) throws InterruptedException {
        int threadCount = 5;
        CountDownLatch latch = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                System.out.println("线程 " + Thread.currentThread().getName() + " 正在运行");
                latch.countDown();
            }).start();
        }

        latch.await(); // 等待所有线程完成
        System.out.println("所有线程已完成,继续执行主线程");
    }
}

11.3 优缺点

12. 使用CyclicBarrier

12.1 基本概念

CyclicBarrier是Java中用于实现线程同步的工具类,它允许一组线程相互等待,直到所有线程都到达某个屏障点后再继续执行。

12.2 示例代码

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierExample {
    public static void main(String[] args) {
        int threadCount = 5;
        CyclicBarrier barrier = new CyclicBarrier(threadCount, () -> {
            System.out.println("所有线程已到达屏障点,继续执行");
        });

        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                System.out.println("线程 " + Thread.currentThread().getName() + " 正在运行");
                try {
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

12.3 优缺点

13. 使用Phaser

13.1 基本概念

Phaser是Java 7引入的一个灵活的同步工具类,它允许线程分阶段地执行任务,并且可以在每个阶段结束时进行同步。

13.2 示例代码

import java.util.concurrent.Phaser;

public class PhaserExample {
    public static void main(String[] args) {
        int threadCount = 5;
        Phaser phaser = new Phaser(threadCount);

        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                System.out.println("线程 " + Thread.currentThread().getName() + " 正在运行");
                phaser.arriveAndAwaitAdvance(); // 到达并等待其他线程
                System.out.println("线程 " + Thread.currentThread().getName() + " 继续执行");
            }).start();
        }
    }
}

13.3 优缺点

14. 使用Semaphore

14.1 基本概念

Semaphore是Java中用于控制并发访问资源的工具类,它通过许可证机制来控制同时访问资源的线程数量。

14.2 示例代码

import java.util.concurrent.Semaphore;

public class SemaphoreExample {
    public static void main(String[] args) {
        int threadCount = 5;
        Semaphore semaphore = new Semaphore(2); // 允许同时访问的线程数量为2

        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire(); // 获取许可证
                    System.out.println("线程 " + Thread.currentThread().getName() + " 正在运行");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release(); // 释放许可证
                    System.out.println("线程 " + Thread.currentThread().getName() + " 已完成");
                }
            }).start();
        }
    }
}

14.3 优缺点

15. 使用Exchanger

15.1 基本概念

Exchanger是Java中用于实现线程间数据交换的工具类,它允许两个线程在某个点交换数据。

15.2 示例代码

import java.util.concurrent.Exchanger;

public class ExchangerExample {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();

        new Thread(() -> {
            try {
                String data = "数据1";
                System.out.println("线程1发送数据: " + data);
                String receivedData = exchanger.exchange(data);
                System.out.println("线程1接收数据: " + receivedData);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                String data = "数据2";
                System.out.println("线程2发送数据: " + data);
                String receivedData = exchanger.exchange(data);
                System.out.println("线程2接收数据: " + receivedData);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

15.3 优缺点

推荐阅读:
  1. Java中如何实现策略模式
  2. Java中怎么利用正则表达式实现条件查询

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:Python怎么使用Qt5实现水平导航栏

下一篇:sql server卡慢问题定位与排查的方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》