OOP20-Java线程间通信

本文介绍了Java中线程间通信的概念,重点讲解了阻塞队列BlockingQueue和非阻塞队列Queue的应用。通过示例展示了如何使用BlockingQueue实现线程间的同步,并指出非阻塞队列Queue在进程通信中的局限性。

多线程-线程间通信

队列
阻塞队列BlockingQueue
线程之间共享阻塞队列BlockingQueue, 作为进程间相互通信的消息队列.
BlockingQueue的方法take(), 当队列的内容为空时, 会阻塞调用此方法的线程(serverThread).

文件ClientThread.java

public class ClientThread {

    private BlockingQueue<String> blockingQueue;

    public static void main(String[] args) {
        ServerThread serverThread = new ServerThread();
        serverThread.start();
        ClientThread clientThread = new ClientThread();
        clientThread.setBlockingQueue(serverThread.getBlockingQueue());
        Scanner scanner = new Scanner(System.in);
        while (serverThread.isAlive()) {
            System.out.println("Input some message:");
            String name = scanner.nextLine();
            clientThread.getBlockingQueue().offer(name);
        }
    }

    public BlockingQueue<String> getBlockingQueue() {
        return blockingQueue;
    }

    public void setBlockingQueue(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }
}

文件ServerThread.java

public class ServerThread extends Thread {

    private BlockingQueue<String> blockingQueue;

    public ServerThread() {
        blockingQueue = new LinkedBlockingQueue<>();
    }

    @Override
    public void run() {
        boolean isExit = false;
        while (isExit == false) {
            try {
                String take = this.blockingQueue.take();
                System.out.println("server get the string: " + take);
                if (take.equals("exit")) {
                    System.out.println("server thread exit");
                    System.out.println("press enter to exit client");
                    isExit = true;
                }
            } catch (InterruptedException ex) {
                System.out.println(ex);
            }
        }
    }

    public BlockingQueue<String> getBlockingQueue() {
        return blockingQueue;
    }

    public void setBlockingQueue(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }
}

运行结果示例:

Input some message:
hi
Input some message:
server get the string: hi
exit
Input some message:
server get the string: exit
server thread exit
press enter to exit client

非阻塞队列Queue
线程之间共享非阻塞队列Queue, 作为进程间相互通信的消息队列.
Queue的方法poll(), 在队列的内容为空时, 会返回null, 而不会阻塞调用此方法的线程.
因此, 非阻塞队列Queue不适合作为进程间相互通信的消息队列.

文件ClientThread.java

public class ClientThread {

    private Queue<String> queue;

    public static void main(String[] args) {
        ServerThread serverThread = new ServerThread();
        serverThread.start();
        ClientThread clientThread = new ClientThread();
        clientThread.setQueue(serverThread.getQueue());
        Scanner scanner = new Scanner(System.in);
        while (serverThread.isAlive()) {
            System.out.println("Input some message:");
            String name = scanner.nextLine();
            clientThread.getQueue().offer(name);
        }
    }

    public Queue<String> getQueue() {
        return queue;
    }

    public void setQueue(Queue<String> queue) {
        this.queue = queue;
    }
}

文件ServerThread.java

public class ServerThread extends Thread {

    private Queue<String> queue;

    public ServerThread() {
        queue = new LinkedList<>();
    }

    @Override
    public void run() {
        boolean isExit = false;
        while (isExit == false) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                System.out.println(ex);
            }
            String data = this.queue.poll();
            if (data != null) {
                System.out.println("server get the string: " + data);
                if (data.equals("exit")) {
                    System.out.println("server thread exit");
                    System.out.println("press enter to exit client");
                    isExit = true;
                }
            } else {
                System.out.println(data);
            }
        }
    }

    public Queue<String> getQueue() {
        return queue;
    }

    public void setQueue(Queue<String> queue) {
        this.queue = queue;
    }
}

运行结果:

Input some message:
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null

如果不打断Ctrl+C, 会不断的在屏幕上打印null.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值