我做了一个,模拟多处理器处理多个请求的程序 Server端只有3个线程,每个线程相当于一个处理器。 request端,生成请求,由Server处理, request如果大于3个,就要等到server端处理器线程空余才执行。 现在问题就是request大于3个的时候,server端怎么处理? import java.util.*; public class Server { public static void main(String args[])throws Exception{ Processor pro = new Processor();
/*Thread t1 = new Processor(); Thread t2 = new Processor(); Thread t3 = new Processor(); t1.start(); //t1.sleep(5000); t2.start(); //t2.sleep(4000); t3.start();*/ int requestNum = pro.req.num; System.out.println(requestNum); Thread []t = new Thread[3]; for(int i=0;i<3;i++){ t[i] = new Thread(pro); }
}class RequestQueue{ static List<Request> list = new ArrayList<Request>(); static int num; static RequestQueue requestqueue = null; private RequestQueue(){ //System.out.println(Math.random()*10/2); //int x = (int)Math.random()*10/2; //System.out.println(x); num = 5;//(int)Math.random()*10/2; //System.out.println(num); for(int i=0;i<num;i++){ String message = i+"request"; list.add(new Request(message)); } } public static synchronized RequestQueue getInstance(){ if(requestqueue == null){ return new RequestQueue(); }else{ return requestqueue; }
}
}class Processor implements Runnable{ static int num = -1; RequestQueue req = RequestQueue.getInstance(); public void run(){ List<Request> ls = req.list; //System.out.println(ls.size()); num ++; ls.get(num).print(); } }
可以继承 Ruannable接口,这样在启用一个线程多次启用~~ public class Jar implements Runnable{
int a = 3; public void run(){ a++; System.out.println(a); }
public static void main(String [] args){ Jar r = new Jar(); new Thread(r).start(); new Thread(r).start(); new Thread(r).start(); String a = new String("a"); String b = new String("a");
int a = 3; public void run(){ a++; System.out.println(a); }
public static void main(String [] args){ Jar r = new Jar(); new Thread(r).start(); new Thread(r).start(); new Thread(r).start(); String a = new String("a"); String b = new String("a");
if(a == b) { System.out.println("="); } if(a.equals(b)) { System.out.println("e"); } } } 打印结果:4 e 5 6
while(true){if(。){break
}}
Server端只有3个线程,每个线程相当于一个处理器。
request端,生成请求,由Server处理,
request如果大于3个,就要等到server端处理器线程空余才执行。
现在问题就是request大于3个的时候,server端怎么处理?
import java.util.*;
public class Server { public static void main(String args[])throws Exception{
Processor pro = new Processor();
/*Thread t1 = new Processor();
Thread t2 = new Processor();
Thread t3 = new Processor();
t1.start();
//t1.sleep(5000);
t2.start();
//t2.sleep(4000);
t3.start();*/
int requestNum = pro.req.num;
System.out.println(requestNum);
Thread []t = new Thread[3];
for(int i=0;i<3;i++){
t[i] = new Thread(pro);
}
for(int i=0;i<requestNum;i++){
t[i%3].start();
}
}
}class Request{
String message;
Request(String s){
message = s;
}
public synchronized void print(){
System.out.println(message);
}
}class RequestQueue{
static List<Request> list = new ArrayList<Request>();
static int num;
static RequestQueue requestqueue = null;
private RequestQueue(){
//System.out.println(Math.random()*10/2);
//int x = (int)Math.random()*10/2;
//System.out.println(x);
num = 5;//(int)Math.random()*10/2;
//System.out.println(num);
for(int i=0;i<num;i++){
String message = i+"request";
list.add(new Request(message));
}
}
public static synchronized RequestQueue getInstance(){
if(requestqueue == null){
return new RequestQueue();
}else{
return requestqueue;
}
}
}class Processor implements Runnable{
static int num = -1;
RequestQueue req = RequestQueue.getInstance();
public void run(){
List<Request> ls = req.list;
//System.out.println(ls.size());
num ++;
ls.get(num).print();
}
}
public class Jar implements Runnable{
int a = 3;
public void run(){
a++;
System.out.println(a);
}
public static void main(String [] args){
Jar r = new Jar();
new Thread(r).start();
new Thread(r).start();
new Thread(r).start();
String a = new String("a");
String b = new String("a");
if(a == b)
{
System.out.println("=");
}
if(a.equals(b))
{
System.out.println("e");
}
}
public class Jar implements Runnable{
int a = 3;
public void run(){
a++;
System.out.println(a);
}
public static void main(String [] args){
Jar r = new Jar();
new Thread(r).start();
new Thread(r).start();
new Thread(r).start();
String a = new String("a");
String b = new String("a");
if(a == b)
{
System.out.println("=");
}
if(a.equals(b))
{
System.out.println("e");
}
}
}
打印结果:4
e
5
6
那么我的想法是你得有个控制器,这个控制器负责接收用户请求,并将请求分发给各个工作线程,这个控制器必须知道三个工作线程目前处于什么生命周期,如果工作线程执行完,则控制器负责重新启动工作线程为用户服务。
这个东西做大了就是个跟Web容器差不多的东西。