最近工作比较闲写了段java线程池方面的东西。别的不说了上代码。
工作线程Woker.java
/**
* @author pxd
*
*/
public class Worker extends Thread {
public boolean isrunning;
public synchronized void setIsRunning(boolean flag){
this.isrunning=flag;
if(flag){
this.notify();
}
}
public boolean getIsrunning() {
return isrunning;
}
public Worker(){
super();
}
public synchronized void run(){
System.out.println("线程"+this.getName()+"初始化成功");
while(true){
if(!isrunning){
try {
System.out.println("工人"+this.getName()+"任务完成回归线程池");
this.wait();
} catch (InterruptedException e) {
System.out.println("线程被阻挡");
e.printStackTrace();
}
}else{
System.out.println("工人"+this.getName()+"开始工作");
try {
this.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("工人"+this.getName()+"任务完成开始休息");
setIsRunning(false);
}
}
}
}
//PoolManage.java 线程池类
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author pxd
*
*/
public class PoolManage {
public int max_pool;
public ArrayList<Worker> init_pools;
public void setMaxPool(int num){
this.max_pool=num;
}
public PoolManage(int num){
setMaxPool(num);
System.out.println("线程池初始化开始。。。");
init_pools=new ArrayList();
for(int i=0;i<this.max_pool;i++){
Worker worker=new Worker();
worker.setName("worker_"+i);
init_pools.add(worker);
worker.start();
}
}
public void process(ArrayList s){
int i;
for(i=0;i<max_pool;i++){
Worker working=init_pools.get(i);
if(!working.isrunning){
System.out.println("工作将由闲置线程"+working.getName()+"执行");
working.setIsRunning(true);
return;
}
}
if(i==max_pool){
System.out.println("线程池无空闲线程,请稍候访问");
}
}
/*public static void main(String[] args){
PoolManage poolManage=new PoolManage(10);
}*/
}
//服务器类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author pxd
*
*/
public class Server {
//public ServerSocket ssocket;
PoolManage pool;
public Server(int num){
pool=new PoolManage(num);//初始化线程池
try{
ServerSocket ssocket=new ServerSocket(2000);
Socket socket=ssocket.accept();
System.out.println("客户端连接成功");
BufferedReader bf=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
String s=null;
while(true){
s=bf.readLine();
//System.out.println("s= "+s);
ArrayList msg=new ArrayList();
msg.add(msg);
pool.process(msg);
}
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
new Server(5);
}
}
//客户端类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
/**
* @author pxd
*
*/
public class Clients {
public static void main(String[] args){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String s;
try {
Socket socket=new Socket("127.0.0.1",2000);
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader sr=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
while((s=sr.readLine())!=null){
out.println(s);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}