Java实现多线程处理队列请求通信:客户端线程向请求队列中不断发生请求,服务器端线程不断处理请求队列中请求。
一个线程(ClientThread)对另一线程(ServerThread)传递请求(request)的实例。实现简单的线程沟通。
设计如下:
Request:请求类,存储请求信息的类。
RequestQueue:请求队列类,接受存储请求的队列类。
ClientThread:发送请求的线程类,负责向请求队列中发送请求。
ServerThread:处理请求的线程类,负责向请求队列中取请求处理。
Mian:程序入口
Request:请求类,存储发生信息的类。
package com.single.queue;
/**
*
* @author Administrator
* 请求类
*/
public class Request {
/**
* 请求名称
*/
private final String name;
public Request(String name){
this.name = name;
}
public String getName(){
return name;
}
public String toString(){
return "{Request."+name+"}";
}
}
Request类中只存放了请求的名字信息。
RequestQueue:请求队列,用例存放请求的队列类。
package com.single.queue;
import java.util.LinkedList;
/**
*
* @author Administrator
* 请求队列
*/
public class RequestQueue {
/**
* 请求队列
*/
private final LinkedList<Request> queue = new LinkedList<Request>();
/**
* 同步取出队列中的请求
* @return
*/
public synchronized Request getRequest(){
while(queue.size()<=0){
try {
System.out.println("{getRequest wait}");
wait(); //如果队列为空,则当前线程进入等待池中
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Request request = queue.removeFirst();
System.out.println(Thread.currentThread().getName()+" getRequest "+request);
return request;
}
/**
* 同步向队列中加入请求
* @param request
*/
public synchronized void putRequest(Request request){
queue.addLast(request);
System.out.println(Thread.currentThread().getName()+" putRequest "+request);
notifyAll(); //队列中加入请求后,换醒等待线程处理请求
}
}
RequestQueue类用来存放请求的类,这个类拥有putRequest和getRequest两个方法;
putRequest方法是实现将请求(request)存放到请求队列中(requestQueue),客户端线程会调用此方法来发生请求并保持到请求队列中。
getRequest方法是实现将请求(request)从请求队列中(requestQueue)中取出,服务器端线程就会调用此方法将请求队列中的请求取出并处理。
ClientThread:客户端线程,发送请求的线程类。
package com.single.queue;
import java.util.Random;
/**
*
* @author Administrator
* 客户端线程负责发送请求
*/
public class ClientThread extends Thread{
/**
* 请求队列
*/
private RequestQueue requestQueue;
/**
* client名称
*/
private String name;
public ClientThread(String name, RequestQueue queue){
super("client."+name);
this.name = name;
this.requestQueue = queue;
}
/**
* 循环加入多个请求
*/
public void run(){
for (int i = 0; i < 10; i++) {
Request request = new Request(name+"."+i);
requestQueue.putRequest(request);
try {
Thread.sleep(new Random().nextInt(1010));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
ServerThread:服务器端线程,处理请求的线程类
package com.single.queue;
import java.util.Random;
/**
*
* @author Administrator
* 服务线程负责处理请求
*/
public class ServerThread extends Thread{
/**
* 请求队列
*/
private RequestQueue requestQueue;
public ServerThread(String name,RequestQueue queue){
super("server."+name);
this.requestQueue = queue;
}
/**
* 循环取出请求
*/
public void run(){
for (int i = 0; i < 10; i++) {
requestQueue.getRequest();
try {
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Main:main程序执行入口
package com.single.queue;
/**
*
* @author Administrator
* main
*/
public class Main {
/**
* 客户端线程请求向队列中不断发送请求,服务器端线程不断处理请求。
* @param args
*/
public static void main(String[] args) {
RequestQueue queue = new RequestQueue();
new ClientThread("a",queue).start();
new ServerThread("a",queue).start();
}
}
执行结果:
client.a putRequest {Request.a.0}
server.a getRequest {Request.a.0}
client.a putRequest {Request.a.1}
server.a getRequest {Request.a.1}
{getRequest wait}
client.a putRequest {Request.a.2}
server.a getRequest {Request.a.2}
client.a putRequest {Request.a.3}
server.a getRequest {Request.a.3}
{getRequest wait}
client.a putRequest {Request.a.4}
server.a getRequest {Request.a.4}
{getRequest wait}
client.a putRequest {Request.a.5}
server.a getRequest {Request.a.5}
{getRequest wait}
client.a putRequest {Request.a.6}
server.a getRequest {Request.a.6}
client.a putRequest {Request.a.7}
client.a putRequest {Request.a.8}
server.a getRequest {Request.a.7}
server.a getRequest {Request.a.8}
{getRequest wait}
client.a putRequest {Request.a.9}
server.a getRequest {Request.a.9}
上面了列子只是一个单线程与单线程通信的列子,如果我们想要看多个线程与多个线程通信,则只需要修改main方法就可以实现:
public static void main(String[] args) {
RequestQueue queue = new RequestQueue();
new ClientThread("a",queue).start();
new ServerThread("a",queue).start();
new ClientThread("b",queue).start();
new ServerThread("b",queue).start();
}
修改后的执行结果如下:
client.a putRequest {Request.a.0}
client.b putRequest {Request.b.0}
server.a getRequest {Request.a.0}
server.b getRequest {Request.b.0}
{getRequest wait}
client.a putRequest {Request.a.1}
server.a getRequest {Request.a.1}
client.b putRequest {Request.b.1}
server.a getRequest {Request.b.1}
{getRequest wait}
client.a putRequest {Request.a.2}
server.b getRequest {Request.a.2}
{getRequest wait}
{getRequest wait}
client.b putRequest {Request.b.2}
server.a getRequest {Request.b.2}
{getRequest wait}
{getRequest wait}
client.b putRequest {Request.b.3}
server.a getRequest {Request.b.3}
{getRequest wait}
{getRequest wait}
client.a putRequest {Request.a.3}
server.a getRequest {Request.a.3}
{getRequest wait}
client.a putRequest {Request.a.4}
server.b getRequest {Request.a.4}
client.b putRequest {Request.b.4}
server.b getRequest {Request.b.4}
client.a putRequest {Request.a.5}
client.a putRequest {Request.a.6}
server.a getRequest {Request.a.5}
……