Java线程发送信号

在Java中,线程是一种轻量级的执行单元,可以并发地执行任务。在多线程编程中,有时候我们需要让线程之间进行通信,以便进行协调和同步操作。而线程发送信号是一种常见的线程通信机制,可以用于线程间的通知和同步。

信号量

信号量是一种用于线程之间通信的机制,它可以控制对共享资源的访问。在Java中,可以使用Semaphore类来实现信号量。

import java.util.concurrent.Semaphore;

public class SemaphoreExample {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(0); // 初始化信号量为0

        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("Thread 1 is waiting...");
                semaphore.acquire(); // 线程1等待信号
                System.out.println("Thread 1 received signal");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                System.out.println("Thread 2 is waiting...");
                semaphore.acquire(); // 线程2等待信号
                System.out.println("Thread 2 received signal");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

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

        // 模拟发送信号
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        semaphore.release(2); // 发送2个信号

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

在上面的示例中,我们创建了一个Semaphore对象,并将其初始化为0。然后我们创建了两个线程thread1thread2,它们都会等待信号。最后,我们使用semaphore.release()方法发送信号。

CountDownLatch

CountDownLatch是另一种线程间通信的机制,它可以用于控制一个或多个线程等待其他线程完成操作后再继续执行。

import java.util.concurrent.CountDownLatch;

public class CountDownLatchExample {
    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(2); // 初始化计数器为2

        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("Thread 1 is waiting...");
                latch.await(); // 线程1等待计数器归零
                System.out.println("Thread 1 resumed");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                System.out.println("Thread 2 is waiting...");
                latch.await(); // 线程2等待计数器归零
                System.out.println("Thread 2 resumed");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

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

        // 模拟执行操作
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown(); // 计数器减一
        latch.countDown(); // 计数器减一

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

在上面的示例中,我们创建了一个CountDownLatch对象,并将其初始化为2。然后我们创建了两个线程thread1thread2,它们都会等待计数器归零。最后,我们使用latch.countDown()方法将计数器减一。

CyclicBarrier

CyclicBarrier也是一种用于线程间通信的机制,它可以让一组线程等待彼此达到共同的执行点,然后再继续执行。

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

public class CyclicBarrierExample {
    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(3); // 初始化屏障为3

        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("Thread 1 is waiting...");
                barrier.await(); // 线程1等待屏障
                System.out.println("Thread 1 resumed");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(()