http://blog.csdn.net/dysj4099/article/details/5983841
任务描述,设计一个程序监听本机8790端口,在有socket连接到来时使用线程池创建处理线程,处理完后返回,主程序不参与服务。
直接贴代码吧:
主控server
- package com.sdc.callmaxent.socket;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- import com.sdc.callmaxent.util.SystemConfig;
- public class CallMaxentThreadPool {
- private static int produceTaskSleepTime = 20;
- private static int produceTaskMaskNumber = 10;
- private static boolean flag = false;
- public static void main(String[] args){
- startServ();
- //构造线程池
- System.out.println("Server listening......");
- int port = SystemConfig.getInstance().getPort();
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(port);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- Socket clientSocket = null;
- ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
- 2, //corePoolSize
- 4, //maximumPoolSize
- 3, //keepAliveTime
- TimeUnit.SECONDS, //unit
- new ArrayBlockingQueue<Runnable>(3), //workQueue
- new ThreadPoolExecutor.DiscardOldestPolicy()//
- );
- int i = 0;
- while(flag){
- //获得客户端请求
- try {
- clientSocket = serverSocket.accept();
- } catch (IOException e) {
- e.printStackTrace();
- }
- String task = "maxent_task@ " + i;
- System.out.println("put " + task);
- threadPool.execute(new CallMaxentThreadPoolTask(clientSocket));
- i++;
- }
- }
- public static void startServ(){
- flag = true;
- }
- public static void stopServ(){
- flag = false;
- }
- }
处理thread程序
- package com.sdc.callmaxent.socket;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.io.Serializable;
- import java.net.Socket;
- import com.sdc.callmaxent.util.FileUtil;
- import com.sdc.callmaxent.util.SocketUtil;
- import com.sdc.callmaxent.util.SystemConfig;
- public class CallMaxentThreadPoolTask implements Runnable, Serializable{
- private static final long serialVersionUID = 0;
- private static int consumeTaskSleepTime = 20000;
- private Socket socket;
- private InputStream clientInput;
- private OutputStream clientOutput;
- CallMaxentThreadPoolTask(Socket socket){
- this.socket = socket;
- try{
- this.clientInput = socket.getInputStream();
- this.clientOutput = socket.getOutputStream();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- public void run(){
- BufferedReader breader = FileUtil.getBufferReader(new InputStreamReader(clientInput));
- try{
- //接收到客户端传来信息
- String strLine = breader.readLine();
- //参数切分
- String[] paramlist = strLine.split(",");
- String maxentPath = SystemConfig.getInstance().getMaxentPath();
- String cmd = "cmd /c java -mx1024m -jar " + maxentPath + "/maxent.jar";
- for(int i = 1;i<paramlist.length;i++){
- cmd += " "+paramlist[i];
- }
- System.out.println(Thread.currentThread().getName());
- System.out.println("Maxent "+paramlist[0]+" is running, please wait!");
- cmd += " novisible autorun";
- Process p = Runtime.getRuntime().exec(cmd);
- p.waitFor();
- PrintWriter printWriter=new PrintWriter(clientOutput,true);
- printWriter.println("Finish!");
- System.out.println("Task "+paramlist[0]+" Finish!");
- }catch (IOException e) {
- e.printStackTrace();
- }catch(InterruptedException e){
- e.printStackTrace();
- }finally{
- FileUtil.close(clientInput);
- FileUtil.close(clientOutput);
- SocketUtil.close(socket);
- }
- }
- }
测试client端
- package com.sdc.callmaxent.socket;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import com.sdc.callmaxent.util.FileUtil;
- import com.sdc.callmaxent.util.SocketUtil;
- import com.sdc.callmaxent.util.SystemConfig;
- public class CallMaxentClient {
- public static void main(String[] args){
- try{
- System.out.println("Start sending......");
- String ipAddress = SystemConfig.getInstance().getIP();
- int port = SystemConfig.getInstance().getPort();
- Socket socket = SocketUtil.getSocket(ipAddress, port);
- BufferedReader bfReader = FileUtil.getBufferReader(new InputStreamReader(System.in));
- System.out.println("Waiting.....");
- String strLine = "1,environmentallayers=F:/test/maxent/testdata/layers,samplesfile=F:/test/maxent/testdata/bradypus.csv,outputdirectory=F:/test/maxent/testdata/outputs,togglelayertype=ecoreg,redoifexists";
- //String strLine = "environmentallayers=F:/test/maxent/testdata/layers,samplesfile=F:/test/maxent/testdata/bradypus.csv,outputdirectory=F:/test/maxent/testdata/outputs2,togglelayertype=ecoreg,redoifexists";
- OutputStream outputStream = socket.getOutputStream();
- InputStream inputStream = socket.getInputStream();
- PrintWriter pw = new PrintWriter(outputStream,true);
- pw.println(strLine);
- BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
- System.out.println(br.readLine());
- }catch (UnknownHostException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Thread Pool 执行优先级
corePoolSize > workQueue > maximumPoolSize -> handler