Java如何实现多线程、线程同步

发布时间:2022-04-27 10:43:22 作者:iii
来源:亿速云 阅读:168

Java如何实现多线程、线程同步

目录

  1. 引言
  2. 多线程基础
  3. 线程同步
  4. 线程通信
  5. 线程池
  6. 高级多线程技术
  7. 总结

引言

在现代计算机系统中,多线程编程已经成为提高程序性能、充分利用多核CPU资源的重要手段。Java作为一门广泛使用的编程语言,提供了丰富的多线程支持。本文将详细介绍Java中如何实现多线程、线程同步以及相关的高级技术。

多线程基础

什么是线程

线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一个进程可以包含多个线程,这些线程共享进程的内存空间和资源。

线程的生命周期

线程的生命周期包括以下几个状态:

  1. 新建(New):线程对象被创建,但尚未启动。
  2. 就绪(Runnable):线程已经启动,等待CPU调度执行。
  3. 运行(Running):线程正在执行。
  4. 阻塞(Blocked):线程因为某些原因(如等待I/O操作、等待锁等)暂时停止执行。
  5. 终止(Terminated):线程执行完毕或被强制终止。

创建线程的方式

Java提供了多种创建线程的方式,下面将详细介绍。

继承Thread类

通过继承Thread类并重写run()方法,可以创建一个新的线程。

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
    }
}

实现Runnable接口

通过实现Runnable接口并重写run()方法,可以将任务与线程分离。

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyRunnable());
        thread.start();
    }
}

实现Callable接口

Callable接口与Runnable接口类似,但它可以返回一个结果,并且可以抛出异常。

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

class MyCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
        return "Thread is running";
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get());
    }
}

使用线程池

线程池是一种管理线程的机制,可以有效地控制线程的数量和执行。

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

public class Main {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new MyRunnable();
            executor.execute(worker);
        }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("All threads are finished");
    }
}

线程同步

为什么需要线程同步

在多线程环境中,多个线程可能会同时访问共享资源,导致数据不一致或程序行为异常。线程同步的目的是确保多个线程在访问共享资源时能够有序地进行,避免竞争条件。

同步方法

通过在方法前加上synchronized关键字,可以将方法声明为同步方法。同一时间只有一个线程可以执行该方法。

class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Count: " + counter.getCount());
    }
}

同步代码块

同步代码块允许更细粒度的控制,只对需要同步的代码进行加锁。

class Counter {
    private int count = 0;
    private final Object lock = new Object();

    public void increment() {
        synchronized (lock) {
            count++;
        }
    }

    public int getCount() {
        return count;
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Count: " + counter.getCount());
    }
}

使用Lock对象

Lock接口提供了比synchronized更灵活的锁机制。ReentrantLockLock接口的一个实现类。

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

class Counter {
    private int count = 0;
    private final Lock lock = new ReentrantLock();

    public void increment() {
        lock.lock();
        try {
            count++;
        } finally {
            lock.unlock();
        }
    }

    public int getCount() {
        return count;
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Count: " + counter.getCount());
    }
}

使用volatile关键字

volatile关键字用于确保变量的可见性。当一个变量被声明为volatile时,线程在读取该变量时会直接从主内存中读取,而不是从线程的本地内存中读取。

class Counter {
    private volatile int count = 0;

    public void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Count: " + counter.getCount());
    }
}

使用原子类

Java提供了一系列原子类(如AtomicIntegerAtomicLong等),这些类提供了原子操作,可以在多线程环境中安全地操作变量。

import java.util.concurrent.atomic.AtomicInteger;

class Counter {
    private AtomicInteger count = new AtomicInteger(0);

    public void increment() {
        count.incrementAndGet();
    }

    public int getCount() {
        return count.get();
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Count: " + counter.getCount());
    }
}

线程通信

wait()和notify()

wait()notify()是Java中用于线程通信的基本方法。wait()使当前线程进入等待状态,直到其他线程调用notify()notifyAll()唤醒它。

class SharedResource {
    private boolean isReady = false;

    public synchronized void waitForReady() throws InterruptedException {
        while (!isReady) {
            wait();
        }
    }

    public synchronized void setReady() {
        isReady = true;
        notifyAll();
    }
}

public class Main {
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();

        Thread t1 = new Thread(() -> {
            try {
                resource.waitForReady();
                System.out.println("Thread 1 is running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(1000);
                resource.setReady();
                System.out.println("Thread 2 is running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
    }
}

Condition对象

Condition对象是Lock接口的一部分,提供了更灵活的线程通信机制。

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

class SharedResource {
    private boolean isReady = false;
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    public void waitForReady() throws InterruptedException {
        lock.lock();
        try {
            while (!isReady) {
                condition.await();
            }
        } finally {
            lock.unlock();
        }
    }

    public void setReady() {
        lock.lock();
        try {
            isReady = true;
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();

        Thread t1 = new Thread(() -> {
            try {
                resource.waitForReady();
                System.out.println("Thread 1 is running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(1000);
                resource.setReady();
                System.out.println("Thread 2 is running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
    }
}

线程池

线程池的优势

线程池的主要优势包括:

  1. 降低资源消耗:通过复用线程,减少线程创建和销毁的开销。
  2. 提高响应速度:任务到达时可以直接执行,无需等待线程创建。
  3. 提高线程的可管理性:线程池可以统一管理线程,避免线程过多导致系统资源耗尽。

线程池的实现

Java提供了ExecutorService接口及其实现类ThreadPoolExecutor来管理线程池。

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

public class Main {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new MyRunnable();
            executor.execute(worker);
        }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("All threads are finished");
    }
}

线程池的配置

线程池的配置参数包括:

  1. 核心线程数(corePoolSize):线程池中保持的最小线程数。
  2. 最大线程数(maximumPoolSize):线程池中允许的最大线程数。
  3. 空闲线程存活时间(keepAliveTime):当线程池中的线程数超过核心线程数时,多余的空闲线程的存活时间。
  4. 任务队列(workQueue):用于存放待执行任务的队列。
import java.util.concurrent.*;

public class Main {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2, // corePoolSize
                4, // maximumPoolSize
                60, // keepAliveTime
                TimeUnit.SECONDS, // unit
                new LinkedBlockingQueue<>(10) // workQueue
        );

        for (int i = 0; i < 10; i++) {
            Runnable worker = new MyRunnable();
            executor.execute(worker);
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("All threads are finished");
    }
}

高级多线程技术

Fork/Join框架

Fork/Join框架是Java 7引入的一个并行计算框架,适用于将一个大任务拆分成多个小任务并行执行的场景。

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

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 Main {
    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool();
        MyTask task = new MyTask(1, 100);
        int result = pool.invoke(task);
        System.out.println("Result: " + result);
    }
}

CompletableFuture

CompletableFuture是Java 8引入的一个类,用于处理异步编程和流式编程。

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

public class Main {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello, World!";
        });

        future.thenAccept(System.out::println);

        System.out.println("Main thread is running");

        future.get();
    }
}

并发集合

Java提供了一系列并发集合类,如ConcurrentHashMapCopyOnWriteArrayList等,这些类在多线程环境中提供了线程安全的操作。

import java.util.concurrent.ConcurrentHashMap;

public class Main {
    public static void main(String[] args) {
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();

        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);

        map.forEach((key, value) -> System.out.println(key + ": " + value));
    }
}

总结

Java提供了丰富的多线程支持,从基础的线程创建、线程同步到高级的线程池、并发集合等,开发者可以根据具体需求选择合适的技术。掌握这些技术不仅可以提高程序的性能,还能确保程序在多线程环境下的正确性和稳定性。希望本文能帮助读者更好地理解和应用Java中的多线程编程技术。

推荐阅读:
  1. 多线程同步基础
  2. 多线程之线程同步

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

java

上一篇:Python的json标准库怎么用

下一篇:Java继承是什么及怎么实现

相关阅读

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

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