マルチスレッドモード(2):Guarded Suspensionモード
6249 ワード
package com.xqi.g_s;
import com.xqi.f.Data;
/**
*
*
* @author mike <br>
* 2015 7 23
*/
public class Request {
private String name;//
private Data response;//
public synchronized Data getResponse() {
return response;
}
public void setResponse(Data response) {
this.response = response;
}
public Request(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "[ Reqeust :" + name + "]";
}
}
2.要求キューの作成
package com.xqi.g_s;
import java.util.LinkedList;
/**
*
*
* @author mike <br>
* 2015 7 23
*/
public class RequestQueue {
//
private LinkedList queue = new LinkedList();
//
public synchronized Request getRequest() {
while (queue.size() == 0) {
try {
wait();// ( ),
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return (Request) queue.remove();// remove() ,
}
public synchronized void addRequest(Request request) {
queue.add(request);
notifyAll();// waiting ,
}
}
3.サーバ処理スレッド
package com.xqi.g_s;
import com.xqi.f.FutureData;
import com.xqi.f.RealData;
/**
*
*
* @author mike <br>
* 2015 7 23
*/
public class ServerThread extends Thread {
private RequestQueue queue;
public ServerThread(RequestQueue queue, String name) {
super(name);
this.queue = queue;
}
@Override
public void run() {
while (true) {
final Request request = queue.getRequest();
final FutureData future = (FutureData) request.getResponse();
RealData realData = new RealData(request.getName());
// future
future.setRealData(realData);
System.out.println("ServerThread Name is: "+Thread.currentThread().getName() + " handles " + request);
}
}
}
4.アナログクライアントスレッド
package com.xqi.g_s;
import java.util.ArrayList;
import java.util.List;
import com.xqi.f.FutureData;
import com.xqi.f.RealData;
/**
*
*
* @author mike <br>
* 2015 7 23
*/
public class ClientThread extends Thread {
private RequestQueue queue;
private List<Request> myRequest = new ArrayList<Request>();
public ClientThread(RequestQueue queue, String name) {
super(name);
this.queue = queue;
}
@Override
public void run() {
//
for (int i = 0; i < 10; i++) {
Request req = new Request("RquestId: " + i + " Thread_Name: " + Thread.currentThread().getName());
System.out.println("ClientThread Name is: "+Thread.currentThread().getName() + " requests " + req);
//
FutureData future =new FutureData();
future.setRealData(new RealData(i+""));
req.setResponse(future);
//
queue.addRequest(req);
myRequest.add(req);
try {
Thread.sleep(1000); //
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.out.println(Thread.currentThread().getName() + " handles " + req);
}
//
for (Request r : myRequest) {
System.out.println("ClientThread Name is :"+Thread.currentThread().getName()+" Response is:"+r.getResponse().getResult());
}
// System.out.println(Thread.currentThread().getName() +" request end");
}
}
5.テスト
package com.xqi.g_s;
/**
*
*
* @author mike <br>
* 2015 7 24
*/
public class GSTest {
public static void main(String[] args) {
RequestQueue queue = new RequestQueue();
// 2x1
for (int i = 0; i < 2; i++) {
new ServerThread(queue, "ServerThread" + i).start();
}
// 5x10
for (int i = 0; i < 5; i++) {
new ClientThread(queue, "ClientThread" + i).start();
}
}
}
6.戻る
.......................................................
ClientThread Name is: ClientThread1 requests [ Reqeust :RquestId: 0 Thread_Name: ClientThread1]
ClientThread Name is: ClientThread0 requests [ Reqeust :RquestId: 0 Thread_Name: ClientThread0]
ClientThread Name is: ClientThread4 requests [ Reqeust :RquestId: 0 Thread_Name: ClientThread4]
ClientThread Name is: ClientThread3 requests [ Reqeust :RquestId: 0 Thread_Name: ClientThread3]
ClientThread Name is: ClientThread2 requests [ Reqeust :RquestId: 0 Thread_Name: ClientThread2]
ServerThread Name is: ServerThread0 handles [ Reqeust :RquestId: 0 Thread_Name: ClientThread2]
ServerThread Name is: ServerThread1 handles [ Reqeust :RquestId: 0 Thread_Name: ClientThread1]
ServerThread Name is: ServerThread0 handles [ Reqeust :RquestId: 0 Thread_Name: ClientThread4]
ServerThread Name is: ServerThread1 handles [ Reqeust :RquestId: 0 Thread_Name: ClientThread3]
ServerThread Name is: ServerThread0 handles [ Reqeust :RquestId: 0 Thread_Name: ClientThread0]
.......................................................
ClientThread Name is :ClientThread3 Response is:0
ClientThread Name is :ClientThread3 Response is:1
ClientThread Name is :ClientThread3 Response is:2
ClientThread Name is :ClientThread3 Response is:3
ClientThread Name is :ClientThread3 Response is:4
ClientThread Name is :ClientThread3 Response is:5
ClientThread Name is :ClientThread3 Response is:6
ClientThread Name is :ClientThread3 Response is:7
ClientThread Name is :ClientThread3 Response is:8
ClientThread Name is :ClientThread3 Response is:9
.......................................................
PS:このDemoはFutureモードで処理します!
最初はwaitとnotifyAllが理解できませんでしたが、スレッドの基礎とインスタンスで何回も実験してみると、分かります!