亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java線程之間通信的幾種方式詳解

 更新時(shí)間:2025年03月21日 11:08:37   作者:yymagicer  
在 Java 中,線程之間的通信是通過(guò)共享內(nèi)存模型來(lái)實(shí)現(xiàn)的,線程通過(guò)共享的對(duì)象和變量來(lái)交換數(shù)據(jù),為了確保線程間通信的正確性,Java 提供了一系列機(jī)制來(lái)實(shí)現(xiàn)線程安全、同步和通信,以下是常用的幾種線程間通信的方式,以及它們的使用方法和場(chǎng)景,需要的朋友可以參考下

1. 共享變量與同步機(jī)制

多個(gè)線程可以通過(guò)共享對(duì)象的變量進(jìn)行通信,但為了避免數(shù)據(jù)不一致的問(wèn)題,必須使用同步機(jī)制來(lái)控制對(duì)共享變量的訪問(wèn)。

  • 使用 synchronized 關(guān)鍵字: synchronized 確保在同一時(shí)刻只有一個(gè)線程可以執(zhí)行同步代碼塊。它可以同步方法或代碼塊,用于保護(hù)共享數(shù)據(jù)。

    示例:

class Counter {
    private int count = 0;
 
    public synchronized void increment() {
        count++;
    }
 
    public synchronized int getCount() {
        return count;
    }
}
 
public class SyncExample {
    public static void main(String[] args) {
        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();
 
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        System.out.println("Final count: " + counter.getCount());
    }
}
  • 使用場(chǎng)景:用于保護(hù)共享資源,防止多個(gè)線程同時(shí)讀寫(xiě)導(dǎo)致數(shù)據(jù)不一致的問(wèn)題。

  • 使用 volatile 關(guān)鍵字: volatile 確保一個(gè)線程對(duì)變量的修改對(duì)其他線程立即可見(jiàn)。它適用于輕量級(jí)的變量同步,通常用于標(biāo)志位控制。

    示例:

class Flag {
    private volatile boolean running = true;
 
    public void stop() {
        running = false;
    }
 
    public boolean isRunning() {
        return running;
    }
}
 
public class VolatileExample {
    public static void main(String[] args) {
        Flag flag = new Flag();
 
        Thread t1 = new Thread(() -> {
            while (flag.isRunning()) {
                System.out.println("Thread is running");
            }
            System.out.println("Thread stopped");
        });
 
        t1.start();
 
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        flag.stop();
    }
}
  • 使用場(chǎng)景:用于控制線程的退出、開(kāi)關(guān)操作等輕量級(jí)場(chǎng)景。

2. wait()、notify() 和 notifyAll()

Object 類的這三個(gè)方法用于在同步塊內(nèi)實(shí)現(xiàn)線程間的協(xié)作。線程可以通過(guò) wait() 進(jìn)入等待狀態(tài),直到另一個(gè)線程調(diào)用 notify() 或 notifyAll() 喚醒它們。

  • wait():當(dāng)前線程進(jìn)入等待狀態(tài),并釋放鎖。
  • notify():?jiǎn)拘岩粋€(gè)正在等待同一鎖的線程。
  • notifyAll():?jiǎn)拘阉械却绘i的線程。

示例:生產(chǎn)者-消費(fèi)者模型

class SharedResource {
    private int value;
    private boolean hasValue = false;
 
    public synchronized void produce(int newValue) throws InterruptedException {
        while (hasValue) {
            wait(); // 等待消費(fèi)者消費(fèi)
        }
        value = newValue;
        hasValue = true;
        System.out.println("Produced: " + value);
        notify(); // 喚醒消費(fèi)者
    }
 
    public synchronized void consume() throws InterruptedException {
        while (!hasValue) {
            wait(); // 等待生產(chǎn)者生產(chǎn)
        }
        System.out.println("Consumed: " + value);
        hasValue = false;
        notify(); // 喚醒生產(chǎn)者
    }
}
 
public class ProducerConsumerExample {
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();
 
        Thread producer = new Thread(() -> {
            try {
                for (int i = 0; i < 5; i++) {
                    resource.produce(i);
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        Thread consumer = new Thread(() -> {
            try {
                for (int i = 0; i < 5; i++) {
                    resource.consume();
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        producer.start();
        consumer.start();
    }
}

使用場(chǎng)景:適用于線程間的協(xié)調(diào)工作,比如生產(chǎn)者-消費(fèi)者模型,一個(gè)線程負(fù)責(zé)生產(chǎn)資源,另一個(gè)線程負(fù)責(zé)消費(fèi)資源。

3. Lock 和 Condition 接口

相比 synchronized,Lock 提供了更靈活的同步機(jī)制,而 Condition 可以替代 wait()notify() 和 notifyAll(),并支持多個(gè)等待條件。

  • ReentrantLock:常用于顯式鎖控制,可以提供公平鎖機(jī)制(按獲取鎖的順序進(jìn)行調(diào)度)。
  • Condition:類似于 Object 的 wait()、notify(),可以創(chuàng)建多個(gè) Condition 來(lái)進(jìn)行復(fù)雜的線程協(xié)調(diào)。

示例:

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
class BoundedBuffer {
    private final int[] buffer;
    private int count, in, out;
    private final Lock lock = new ReentrantLock();
    private final Condition notFull = lock.newCondition();
    private final Condition notEmpty = lock.newCondition();
 
    public BoundedBuffer(int size) {
        buffer = new int[size];
    }
 
    public void put(int value) throws InterruptedException {
        lock.lock();
        try {
            while (count == buffer.length) {
                notFull.await(); // 等待緩沖區(qū)未滿
            }
            buffer[in] = value;
            in = (in + 1) % buffer.length;
            count++;
            notEmpty.signal(); // 喚醒消費(fèi)線程
        } finally {
            lock.unlock();
        }
    }
 
    public int take() throws InterruptedException {
        lock.lock();
        try {
            while (count == 0) {
                notEmpty.await(); // 等待緩沖區(qū)非空
            }
            int value = buffer[out];
            out = (out + 1) % buffer.length;
            count--;
            notFull.signal(); // 喚醒生產(chǎn)線程
            return value;
        } finally {
            lock.unlock();
        }
    }
}
 
public class LockConditionExample {
    public static void main(String[] args) {
        BoundedBuffer buffer = new BoundedBuffer(5);
 
        Thread producer = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    buffer.put(i);
                    System.out.println("Produced: " + i);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        Thread consumer = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    int value = buffer.take();
                    System.out.println("Consumed: " + value);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        producer.start();
        consumer.start();
    }
}

使用場(chǎng)景:適用于需要顯式鎖定、更復(fù)雜的條件等待場(chǎng)景,比如多條件同步、多生產(chǎn)者-消費(fèi)者模型。

4. java.util.concurrent 包的并發(fā)工具

Java 提供了 java.util.concurrent 包下的一系列高級(jí)并發(fā)工具類來(lái)簡(jiǎn)化線程間通信。

  • BlockingQueue:線程安全的隊(duì)列,常用于生產(chǎn)者-消費(fèi)者模式。
  • CountDownLatch:允許一個(gè)或多個(gè)線程等待其他線程完成某些操作。
  • CyclicBarrier:多個(gè)線程在某個(gè)點(diǎn)上相互等待,直到所有線程到達(dá)該點(diǎn)。
  • Semaphore:用于控制對(duì)資源的訪問(wèn)許可。
  • Exchanger:用于兩個(gè)線程之間交換數(shù)據(jù)。

到此這篇關(guān)于Java線程之間通信的幾種方式詳解的文章就介紹到這了,更多相關(guān)Java線程之間通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談Java并發(fā)之同步器設(shè)計(jì)

    淺談Java并發(fā)之同步器設(shè)計(jì)

    這篇文章主要介紹Java并發(fā)之同步器設(shè)計(jì),本文以記錄方式并發(fā)編程中同步器設(shè)計(jì)的一些共性特征。并簡(jiǎn)單介紹了Java中的AQS,需要的朋友可以參考一下文章的詳細(xì)內(nèi)容
    2021-10-10
  • java中Optional的使用詳細(xì)解析

    java中Optional的使用詳細(xì)解析

    這篇文章主要介紹了java新特性之Optional的詳細(xì)解析,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序

    聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序

    這篇文章主要介紹了聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Thread類interrupt interrupted及isInterrupted區(qū)別

    Thread類interrupt interrupted及isInterrupted區(qū)別

    這篇文章主要為大家介紹了Thread類interrupt interrupted及isInterrupted區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Spring-ImportSelector接口功能使用案例

    Spring-ImportSelector接口功能使用案例

    ImportSelector接口是至spring中導(dǎo)入內(nèi)部類或者外部類的核心接口,只需要其定義的方法內(nèi)返回需要?jiǎng)?chuàng)建bean的class字符串就好了,這篇文章主要介紹了Spring-ImportSelector接口功能介紹,需要的朋友可以參考下
    2023-09-09
  • Spring @Async無(wú)法實(shí)現(xiàn)異步的解決方案

    Spring @Async無(wú)法實(shí)現(xiàn)異步的解決方案

    這篇文章主要介紹了Spring @Async無(wú)法實(shí)現(xiàn)異步的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot之Order注解啟動(dòng)順序說(shuō)明

    SpringBoot之Order注解啟動(dòng)順序說(shuō)明

    這篇文章主要介紹了SpringBoot之Order注解啟動(dòng)順序說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java中的對(duì)象和對(duì)象引用實(shí)例淺析

    Java中的對(duì)象和對(duì)象引用實(shí)例淺析

    這篇文章主要介紹了Java中的對(duì)象和對(duì)象引用,實(shí)例分析了對(duì)象與對(duì)象引用的概念與相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • 簡(jiǎn)單實(shí)現(xiàn)java抽獎(jiǎng)系統(tǒng)

    簡(jiǎn)單實(shí)現(xiàn)java抽獎(jiǎng)系統(tǒng)

    這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)java抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Java解析XML的四種方法詳解

    Java解析XML的四種方法詳解

    XML現(xiàn)在已經(jīng)成為一種通用的數(shù)據(jù)交換格式,平臺(tái)的無(wú)關(guān)性使得很多場(chǎng)合都需要用到XML。本文將詳細(xì)介紹用Java解析XML的四種方法
    2012-10-10

最新評(píng)論