java多线程-GuardedSuspension保护暂停模式

写在前面的话:假设一种情景,假设你在家里换衣服,这个时候有顺风快递员在门外敲门,你会说等一下,换完衣服你去开门。假设你换衣服是一个线程,顺风快递员把东西送到你的手机是另一个线程,很明显后者线程要目的达到需要前者线程的执行完的条件,这个“保护暂停模式”就是应这样的需求而生。

下面是实例程序:
涉及类及作用介绍:
1,Request:用来表示请求的类。
2,RequestQueue:依次存放请求以待使用的类。
3,ClientRequest:送出请求的类。
4,ServerThread:接收请求的类。
5,Main:操作测试类。

类Request:
package GuardedSuspensionPattern;
/*
*该类用来表示请求
*/
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 + "]";
}
}


类RequestQueue:
package GuardedSuspensionPattern;
import java.util.LinkedList;
/*
*请求队列类,有放入请求方法,拿出请求方法,注意都是synchronized的。
*放入的方法没有限制,取出的方法就要先判断有没有请求了,如果没有请求就一直等到有请求。
*/
public class RequestQueue {
private final LinkedList queue = new LinkedList();
//取出请求的方法
public synchronized Request getRequest(){
while(queue.size() <= 0){
try {
wait();//一旦请求队列中没有请求,持有RequestQueue实例的线程放入到RequestQueue实例的线程等待区(一旦进入到该区就释放了之前的锁)
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return queue.removeFirst();//拿出队列的第一个请求并返回该请求
}
//放入请求的方法,一旦放入请求,就唤醒所有RequestQueue实例线程等待区的所有等待线程
public synchronized void putRequest(Request request){
queue.addLast(request);//请新的请求放入到队列的最后
notifyAll();//唤醒所有等待的线程
}
}



类:ClientThread
package GuardedSuspensionPattern;
import java.util.Random;
/*
* 该线程类主要负责生产一千个请求放进请求队列中,供处理线程处理
*/
public class ClientThread extends Thread{
private Random random;
private RequestQueue requestQueue;
public ClientThread(RequestQueue requestQueue,String name,long seed){
this.requestQueue = requestQueue;
this.random = new Random(seed);
}
public void run(){
for(int i=0; i<10000; i++){
Request request = new Request("NO:" + i);
System.out.println(Thread.currentThread().getName() + "requests: " + request);
requestQueue.putRequest(request);
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}



类ServerThread:
package GuardedSuspensionPattern;
import java.util.Random;
/*&
* 该线程主要带请求队列中拿出一千个请求来处理,如果请求队列中没有请求,就一直等到
* 有请求为止。
*/
public class ServerThread extends Thread{
private Random random;
private RequestQueue requestQueue;
public ServerThread(RequestQueue requestQueue,String name,long seed){
this.requestQueue = requestQueue;
this.random = new Random(seed);
}
public void run(){
for(int i = 0; i < 10000; i++){
Request request = requestQueue.getRequest();
System.out.println(Thread.currentThread().getName() + "handles:" + request);
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}



类Main
package GuardedSuspensionPattern;
public class Main {

public static void main(String[] args) {
//定义请求队列实例,注意该实例就是共享资源
RequestQueue requestQueue = new RequestQueue();
//开启“产生1000个请求的实例”线程
new ClientThread(requestQueue,"Supan1",3141592L).start();
//开始“处理1000个请求的实例“线程
new ServerThread(requestQueue, "Supan2", 6535897L).start();
}
}


运行结果:
Thread-0requests: [Request:NO:0]
Thread-1handles:[Request:NO:0]
Thread-0requests: [Request:NO:1]
Thread-0requests: [Request:NO:2]
Thread-1handles:[Request:NO:1]
Thread-1handles:[Request:NO:2]
Thread-0requests: [Request:NO:3]
Thread-1handles:[Request:NO:3]
Thread-0requests: [Request:NO:4]
Thread-1handles:[Request:NO:4]
Thread-0requests: [Request:NO:5]
Thread-0requests: [Request:NO:6]
Thread-1handles:[Request:NO:5]
Thread-1handles:[Request:NO:6]
Thread-0requests: [Request:NO:7]
Thread-1handles:[Request:NO:7]
Thread-0requests: [Request:NO:8]
Thread-1handles:[Request:NO:8]
Thread-0requests: [Request:NO:9]
Thread-1handles:[Request:NO:9]
Thread-0requests: [Request:NO:10]
Thread-0requests: [Request:NO:11]
Thread-1handles:[Request:NO:10]
Thread-1handles:[Request:NO:11]
Thread-0requests: [Request:NO:12]
Thread-1handles:[Request:NO:12]
Thread-0requests: [Request:NO:13]
Thread-1handles:[Request:NO:13]
Thread-0requests: [Request:NO:14]
Thread-1handles:[Request:NO:14]
Thread-0requests: [Request:NO:15]
Thread-1handles:[Request:NO:15]
Thread-0requests: [Request:NO:16]
Thread-1handles:[Request:NO:16]
Thread-0requests: [Request:NO:17]
Thread-1handles:[Request:NO:17]
Thread-0requests: [Request:NO:18]
Thread-1handles:[Request:NO:18]
Thread-0requests: [Request:NO:19]
Thread-1handles:[Request:NO:19]

你可能感兴趣的:(java多线程,java多线程)