Java提供了一个很好地类来实现1中的线程池功能就是Executors类。将服务端代码改变一下就OK了。当然了,ReceiveFile类还需要改变一下(实现一个Runnable接口,并将receiveFile方法的内容写到run方法中,删除receiveFile方法即可)。
package com.wjy.threadpool; import java.io.BufferedInputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.*; import com.wjy.receivefile.ReceiveFile; public class ThreadPool { private int port; private int poolSize=0; private ServerSocket server; public ThreadPool(int port, int poolSize) { super(); this.port = port; this.poolSize = poolSize; } private void startServer(){ try { server=new ServerSocket(port); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private int fileNum(){ int result=(int)(Math.random()*10); return result; } public void startThreadPool(){ startServer(); Executor service=Executors.newCachedThreadPool(); while(true){ try { Socket client=server.accept(); int num=fileNum(); System.out.println(num); BufferedInputStream inputStream=new BufferedInputStream(client.getInputStream()); Thread.sleep(15000); service.execute(new ReceiveFile(inputStream, "E://receive/"+num+".txt")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // for(int i=0;i<poolSize;i++){ // Thread thread=new Thread(){ // public void run(){ // while(true){ // try { // Socket client=server.accept(); // int num=fileNum(); // System.out.println(num); // BufferedInputStream inputStream=new BufferedInputStream(client.getInputStream()); // ReceiveFile receiveFile=new ReceiveFile(inputStream, "E://receive/"+num+".txt"); // Thread.sleep(15000); // receiveFile.receiveFile(); // } catch (Exception e) { // e.printStackTrace(); // } // } // } // }; // thread.start(); // } } }
接受文件的类:
package com.wjy.receivefile; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class ReceiveFile implements Runnable{ private int buffSize=1024; private BufferedInputStream conveyInputStream; private BufferedOutputStream fileOutputStream; public void setBuffSize(int buffSize) { this.buffSize = buffSize; } public ReceiveFile(BufferedInputStream conveyInputStream, String filePath) { super(); this.conveyInputStream = conveyInputStream; try { this.fileOutputStream=new BufferedOutputStream(new FileOutputStream(new File(filePath))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // public void receiveFile(){ // byte buff[]=new byte[buffSize]; // try { // while((conveyInputStream.read(buff, 1, buffSize-1))!=-1){ // fileOutputStream.write(buff, 1, buffSize-1); // } // conveyInputStream.close(); // fileOutputStream.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } @Override public void run() { // TODO Auto-generated method stub byte buff[]=new byte[buffSize]; try { while((conveyInputStream.read(buff, 1, buffSize-1))!=-1){ fileOutputStream.write(buff, 1, buffSize-1); } conveyInputStream.close(); fileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }