多线程-线程间通信
队列
阻塞队列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.