Java中线程间通信和数据共享

概述

在Java中,线程间通信和数据共享是实现多线程编程的重要组成部分。为了使多个线程能够安全地访问共享数据并进行有效的通信,我们需要使用一些机制来确保线程之间的顺序和数据的一致性。本文将介绍Java中几种常见的线程间通信和数据共享的解决方案,并提供相应的代码示例。

一、使用synchronized关键字实现线程间通信和数据共享

使用synchronized方法
synchronized关键字可以修饰方法,使得只有一个线程可以进入该方法执行,其他线程需要等待。这可以用于实现线程之间的通信和数据共享。

示例代码:

public class ThreadCommunication {
    private boolean flag = false;
    public synchronized void printNumbers() {
        // 线程A负责打印奇数
        for (int i = 1; i <= 10; i += 2) {
            while (flag) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("ThreadA: " + i);
            flag = true;
            notifyAll();
        }
    }
    public synchronized void printLetters() {
        // 线程B负责打印偶数
        for (char c = 'A'; c <= 'J'; c += 2) {
            while (!flag) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("ThreadB: " + c);
            flag = false;
            notifyAll();
        }
    }
    public static void main(String[] args) {
        final ThreadCommunication communication = new ThreadCommunication();
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                communication.printNumbers();
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                communication.printLetters();
            }
        });
        threadA.start();
        threadB.start();
    }
}

在上述示例中,通过使用synchronized关键字修饰printNumbers()和printLetters()方法,确保了线程A和线程B之间的顺序和共享数据的一致性。使用flag标志位控制两个线程的交替执行,通过wait()和notifyAll()方法进行线程的互斥和通信。

使用synchronized块
synchronized关键字也可以修饰代码块,使得只有一个线程可以进入该代码块执行,其他线程需要等待。这可以用于实现线程间的通信和数据共享。

示例代码:

public class ThreadCommunication2 {
    private Object lock = new Object();
    private int number = 0;
    public void printNumbers() {
        synchronized (lock) {
            // 线程A负责打印奇数
            for (int i = 1; i <= 10; i += 2) {
                while (number % 2 == 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("ThreadA: " + i);
                number++;
                lock.notifyAll();
            }
        }
    }
    public void printLetters() {
        synchronized (lock) {
            // 线程B负责打印偶数
            for (char c = 'A'; c <= 'J'; c += 2) {
                while (number % 2 != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("ThreadB: " + c);
                number++;
                lock.notifyAll();
            }
        }
    }
    public static void main(String[] args) {
        final ThreadCommunication2 communication = new ThreadCommunication2();
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                communication.printNumbers();
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                communication.printLetters();
            }
        });
        threadA.start();
        threadB.start();
    }
}

在上述示例中,通过使用synchronized关键字修饰代码块,确保了线程A和线程B之间的顺序和共享数据的一致性。使用number变量和lock对象控制两个线程的交替执行,通过wait()和notifyAll()方法进行线程的互斥和通信。

二、使用Lock和Condition实现线程间通信和数据共享

使用ReentrantLock和Condition
ReentrantLock是Java提供的可重入的互斥锁,可以用于实现线程间的通信和数据共享。Condition是ReentrantLock提供的条件对象,可以通过其中的await()和signalAll()方法实现线程的阻塞和唤醒。

示例代码:

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadCommunication3 {
    private Lock lock = new ReentrantLock();
    private Condition numberCondition = lock.newCondition();
    private Condition letterCondition = lock.newCondition();
    private int number = 0;
    public void printNumbers() {
        lock.lock();
        try {
            // 线程A负责打印奇数
            for (int i = 1; i <= 10; i += 2) {
                while (number % 2 == 0) {
                    try {
                        numberCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("ThreadA: " + i);
                number++;
                letterCondition.signalAll();
            }
        } finally {
            lock.unlock();
        }
    }
    public void printLetters() {
        lock.lock();
        try {
            // 线程B负责打印偶数
            for (char c = 'A'; c <= 'J'; c += 2) {
                while (number % 2 != 0) {
                    try {
                        letterCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("ThreadB: " + c);
                number++;
                numberCondition.signalAll();
            }
        } finally {
            lock.unlock();
        }
    }
    public static void main(String[] args) {
        final ThreadCommunication3 communication = new ThreadCommunication3();
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                communication.printNumbers();
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                communication.printLetters();
            }
        });
        threadA.start();
        threadB.start();
    }
}

在上述示例中,通过使用ReentrantLock和Condition实现了线程A和线程B之间的顺序和共享数据的一致性。使用number变量、lock对象和Condition对象控制两个线程的交替执行,通过await()和signalAll()方法进行线程的阻塞和唤醒。

三、使用volatile关键字实现线程间的数据共享

volatile关键字可用于修饰变量,保证了变量对所有线程的可见性。当一个线程修改了一个volatile变量的值,其他线程会立即看到最新的值,从而保证了数据的一致性。

示例代码:

public class ThreadCommunication4 {
    private volatile boolean flag = false;
    public void printNumbers() {
        // 线程A负责打印奇数
        for (int i = 1; i <= 10; i += 2) {
            while (flag) {
                // 空循环,等待flag为false
            }
            System.out.println("ThreadA: " + i);
            flag = true;
        }
    }
    public void printLetters() {
        // 线程B负责打印偶数
        for (char c = 'A'; c <= 'J'; c += 2) {
            while (!flag) {
                // 空循环,等待flag为true
            }
            System.out.println("ThreadB: " + c);
            flag = false;
        }
    }
    public static void main(String[] args) {
        final ThreadCommunication4 communication = new ThreadCommunication4();
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                communication.printNumbers();
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                communication.printLetters();
            }
        });
        threadA.start();
        threadB.start();
    }
}
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值