Java线程之间如何通信的,有哪些方式?

您所在的位置:网站首页 养生的几种方式包括哪些呢 Java线程之间如何通信的,有哪些方式?

Java线程之间如何通信的,有哪些方式?

#Java线程之间如何通信的,有哪些方式?| 来源: 网络整理| 查看: 265

 

线程之间的通信方式主要有以下几种:

共享变量:线程之间可以通过共享变量来进行通信。不同的线程可以共享同一个变量,并在变量上进行读写操作。需要注意的是,共享变量可能会引发线程安全问题,需要通过同步机制来确保线程安全。

锁机制:锁机制是一种常用的线程同步机制,可以保证在同一时间只有一个线程能够访问共享资源。Java提供了多种锁类型,如 synchronized 关键字、ReentrantLock 类等。

条件变量:条件变量是一种线程间通信机制,它用于在一个共享资源上等待某个条件的成立。Java 提供了 Condition 接口来支持条件变量的实现,在使用 Condition 时需要先获取锁,然后调用 await() 方法等待条件成立,当条件成立时可以通过 signal() 或 signalAll() 方法唤醒等待该条件的线程。

信号量:信号量是一种常见的线程同步机制,可用于控制多个线程对共享资源的访问。Java 提供了 Semaphore 类来实现信号量,Semaphore 类有两个常用的方法 acquire() 和 release(),分别用于获取和释放信号量。

管道:管道是一种用于线程间通信的高级机制,它可以实现一个线程向另一个线程传送数据。Java 提供了 PipedInputStream 和 PipedOutputStream 两个类来支持管道的实现,其中 PipedInputStream 用于读取数据,PipedOutputStream 用于写入数据。

需要注意的是,以上通信方式都需要在多线程程序中谨慎使用,需要考虑线程安全和性能等方面的问题。为了确保程序正确、高效地运行,需要根据具体情况选择合适的线程通信方式,并进行相应的测试和优化。

具体的示例 共享变量 public class SharedData { private int value; public synchronized int getValue() { return value; } public synchronized void setValue(int value) { this.value = value; } }

        在这个示例中,定义了一个共享数据类 SharedData,其中包含一个整型变量 value 和两个同步方法 getValue() 和 setValue(),用于获取和设置变量的值。由于这两个方法都是同步的,因此多个线程可以安全地访问该变量。

public class SharedDataExample { public static void main(String[] args) throws InterruptedException { SharedData sharedData = new SharedData(); Thread thread1 = new Thread(() -> { for (int i = 0; i < 10; i++) { sharedData.setValue(i); System.out.println(Thread.currentThread().getName() + " write " + sharedData.getValue()); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread thread2 = new Thread(() -> { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + " read " + sharedData.getValue()); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); } }

        在这个示例中,创建了两个线程分别用于读写共享数据 SharedData,多次执行该示例可以看到控制台输出表明两个线程在安全地访问共享变量。

锁机制 public class LockExample { private static Lock lock = new ReentrantLock(); private static int count = 0; private static void increase() { lock.lock(); try { count++; } finally { lock.unlock(); } } public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(() -> { for (int i = 0; i < 10000; i++) { increase(); } }); Thread thread2 = new Thread(() -> { for (int i = 0; i < 10000; i++) { increase(); } }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); System.out.println(count); } }

        在这个示例中,使用了 Lock 接口和 ReentrantLock 类来对计数器进行同步,多次执行该示例可以看到最终输出的计数器值为 20000。

条件变量 public class ConditionExample { private static Lock lock = new ReentrantLock(); private static Condition condition = lock.newCondition(); private static int count = 0; private static void await() throws InterruptedException { lock.lock(); try { condition.await(); } finally { lock.unlock(); } } private static void signal() { lock.lock(); try { condition.signalAll(); } finally { lock.unlock(); } } public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(() -> { for (int i = 0; i < 10; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } count++; System.out.println(Thread.currentThread().getName() + " increase count to " + count); if (count == 5) { signal(); } } }); Thread thread2 = new Thread(() -> { try { await(); System.out.println(Thread.currentThread().getName() + " receive signal, count is " + count); } catch (InterruptedException e) { e.printStackTrace(); } }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); } }

        在这个示例中,使用了 Lock 接口和 Condition 接口来定义了一个计数器,线程1每次增加计数器的值并判断是否达到条件,当计数器达到条件时调用 signal() 方法通知线程2,线程2等待条件成立后执行相应的操作。

信号量 public class SemaphoreExample { private static Semaphore semaphore = new Semaphore(2); private static void doWork() throws InterruptedException { semaphore.acquire(); System.out.println(Thread.currentThread().getName() + " start working"); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + " finish working"); semaphore.release(); } public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(() -> { try { doWork(); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread thread2 = new Thread(() -> { try { doWork(); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread thread3 = new Thread(() -> { try { doWork(); } catch (InterruptedException e) { e.printStackTrace(); } }); thread1.start(); thread2.start(); thread3.start(); thread1.join(); thread2.join(); thread3.join(); } }

        在这个示例中,使用了 Semaphore 类来定义了一个信号量,线程1、线程2、线程3都需要获取信号量才能进行工作,每次执行 doWork() 方法需要占用资源,执行完毕后释放信号量。

管道 public class PipeExample { static class WriterThread extends Thread { private PipedOutputStream output; WriterThread(PipedOutputStream output) { this.output = output; } @Override public void run() { try { for(int i=1;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3