❶ java socket傳送文件一直被阻塞
是不能等於-1撒..
他在等你那邊給他寫東西呢..
你應該在伺服器端結束的時候給他寫個東西過去..讓他知道已經結束了..
還有什麼問題HI我哈
但是read方法本身不就有告知客戶端文件傳送結束的功能么 當讀到文件結束符的時候它會返回-1的啊
確實讀文件結束就是-1...
但是你的客戶端讀的不是文件啊..伺服器才是讀文件..所以伺服器能正常結束..
你的客戶端讀的伺服器發來的東西..伺服器讀文件結束後就不給客戶端發信息了..
而客戶端的read()方法是阻塞式方法..意思就是伺服器不傳給他數據他就會一直等..
所以還是那樣..在伺服器端結束的時候給客戶端發個消息說明已經結束了..客戶端讀到這個結束標志的時候也就不要再往文件裡面寫東西了..也結束..這樣你的程序就正確了..
❷ 關於用JAVA的SOCKET傳輸文件
點對點傳輸文件
/*
import java.io.*;
import java.net.*;
import java.util.*;
*/
private HttpURLConnection connection;//存儲連接
private int downsize = -1;//下載文件大小,初始值為-1
private int downed = 0;//文加已下載大小,初始值為0
private RandomAccessFile savefile;//記錄下載信息存儲文件
private URL fileurl;//記錄要下載文件的地址
private DataInputStream fileStream;//記錄下載的數據流
try{
/*開始創建下載的存儲文件,並初始化值*/
File tempfileobject = new File("h:\\webwork-2.1.7.zip");
if(!tempfileobject.exists()){
/*文件不存在則建立*/
tempfileobject.createNewFile();
}
savefile = new RandomAccessFile(tempfileobject,"rw");
/*建立連接*/
fileurl = new URL("https://webwork.dev.java.net/files/documents/693/9723/webwork-2.1.7.zip");
connection = (HttpURLConnection)fileurl.openConnection();
connection.setRequestProperty("Range","byte="+this.downed+"-");
this.downsize = connection.getContentLength();
//System.out.println(connection.getContentLength());
new Thread(this).start();
}
catch(Exception e){
System.out.println(e.toString());
System.out.println("構建器錯誤");
System.exit(0);
}
public void run(){
/*開始下載文件,以下測試非斷點續傳,下載的文件存在問題*/
try{
System.out.println("begin!");
Date begintime = new Date();
begintime.setTime(new Date().getTime());
byte[] filebyte;
int onecelen;
//System.out.println(this.connection.getInputStream().getClass().getName());
this.fileStream = new DataInputStream(
new BufferedInputStream(
this.connection.getInputStream()));
System.out.println("size = " + this.downsize);
while(this.downsize != this.downed){
if(this.downsize - this.downed > 262144){//設置為最大256KB的緩存
filebyte = new byte[262144];
onecelen = 262144;
}
else{
filebyte = new byte[this.downsize - this.downed];
onecelen = this.downsize - this.downed;
}
onecelen = this.fileStream.read(filebyte,0,onecelen);
this.savefile.write(filebyte,0,onecelen);
this.downed += onecelen;
System.out.println(this.downed);
}
this.savefile.close();
System.out.println("end!");
System.out.println(begintime.getTime());
System.out.println(new Date().getTime());
System.out.println(begintime.getTime() - new Date().getTime());
}
catch(Exception e){
System.out.println(e.toString());
System.out.println("run()方法有問題!");
}
}
/***
//FileClient.java
import java.io.*;
import java.net.*;
public class FileClient {
public static void main(String[] args) throws Exception {
//使用本地文件系統接受網路數據並存為新文件
File file = new File("d:\\fmd.doc");
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 通過Socket連接文件伺服器
Socket server = new Socket(InetAddress.getLocalHost(), 3318);
//創建網路接受流接受伺服器文件數據
InputStream netIn = server.getInputStream();
InputStream in = new DataInputStream(new BufferedInputStream(netIn));
//創建緩沖區緩沖網路數據
byte[] buf = new byte[2048];
int num = in.read(buf);
while (num != (-1)) {//是否讀完所有數據
raf.write(buf, 0, num);//將數據寫往文件
raf.skipBytes(num);//順序寫文件位元組
num = in.read(buf);//繼續從網路中讀取文件
}
in.close();
raf.close();
}
}
//FileServer.java
import java.io.*;
import java.util.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) throws Exception {
//創建文件流用來讀取文件中的數據
File file = new File("d:\\系統特點.doc");
FileInputStream fos = new FileInputStream(file);
//創建網路伺服器接受客戶請求
ServerSocket ss = new ServerSocket(8801);
Socket client = ss.accept();
//創建網路輸出流並提供數據包裝器
OutputStream netOut = client.getOutputStream();
OutputStream doc = new DataOutputStream(
new BufferedOutputStream(netOut));
//創建文件讀取緩沖區
byte[] buf = new byte[2048];
int num = fos.read(buf);
while (num != (-1)) {//是否讀完文件
doc.write(buf, 0, num);//把文件數據寫出網路緩沖區
doc.flush();//刷新緩沖區把數據寫往客戶端
num = fos.read(buf);//繼續從文件中讀取數據
}
fos.close();
doc.close();
}
}
*/
❸ 怎麼用java的socket進行文件傳輸誰能給個簡單的例子,包括發送端和接收端。
java中的網路信息傳輸方式是基於TCP協議或者UD協議P的,socket是基於TCP協議的
例子1
(1)客戶端程序:
import java.io.*;
import java.net.*;
public class Client
{ public static void main(String args[])
{ String s=null;
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try{
mysocket=new Socket("localhost",4331);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
out.writeUTF("你好!");//通過 out向"線路"寫入信息。
while(true)
{
s=in.readUTF();//通過使用in讀取伺服器放入"線路"里的信息。堵塞狀態,
//除非讀取到信息。
out.writeUTF(":"+Math.random());
System.out.println("客戶收到:"+s);
Thread.sleep(500);
}
}
catch(IOException e)
{ System.out.println("無法連接");
}
catch(InterruptedException e){}
}
}
(2)伺服器端程序:
import java.io.*;import java.net.*;
public class Server
{ public static void main(String args[])
{ ServerSocket server=null;
Socket you=null;String s=null;
DataOutputStream out=null;DataInputStream in=null;
try{ server=new ServerSocket(4331);}
catch(IOException e1){System.out.println("ERRO:"+e1);}
try{ you=server.accept();
in=new DataInputStream(you.getInputStream());
out=new DataOutputStream(you.getOutputStream());
while(true)
{
s=in.readUTF();// 通過使用in讀取客戶放入"線路"里的信息。堵塞狀態,
//除非讀取到信息。
out.writeUTF("你好:我是伺服器");//通過 out向"線路"寫入信息.
out.writeUTF("你說的數是:"+s);
System.out.println("伺服器收到:"+s);
Thread.sleep(500);
}
}
catch(IOException e)
{ System.out.println(""+e);
}
catch(InterruptedException e){}
}
}
例子(2)
(1) 客戶端
import java.net.*;import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
public class Computer_client extends Applet implements Runnable,ActionListener
{ Button 計算;TextField 輸入三邊長度文本框,計算結果文本框;
Socket socket=null;
DataInputStream in=null; DataOutputStream out=null;
Thread thread;
public void init()
{ setLayout(new GridLayout(2,2));
Panel p1=new Panel(),p2=new Panel();
計算=new Button(" 計算");
輸入三邊長度文本框=new TextField(12);計算結果文本框=new TextField(12);
p1.add(new Label("輸入三角形三邊的長度,用逗號或空格分隔:"));
p1.add( 輸入三邊長度文本框);
p2.add(new Label("計算結果:"));p2.add(計算結果文本框);p2.add(計算);
計算.addActionListener(this);
add(p1);add(p2);
}
public void start()
{ try
{ //和小程序所駐留的伺服器建立套接字連接:
socket = new Socket(this.getCodeBase().getHost(), 4331);
in =new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
if(thread == null)
{ thread = new Thread(this);
thread.start();
}
}
public void run()
{ String s=null;
while(true)
{ try{ s=in.readUTF();//堵塞狀態,除非讀取到信息。
計算結果文本框.setText(s);
}
catch(IOException e)
{ 計算結果文本框.setText("與伺服器已斷開");
break;
}
}
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==計算)
{ String s=輸入三邊長度文本框.getText();
if(s!=null)
{ try { out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
}
(2) 伺服器端
import java.io.*;import java.net.*;
import java.util.*;import java.sql.*;
public class Computer_server
{ public static void main(String args[])
{ ServerSocket server=null;Server_thread thread;
Socket you=null;
while(true)
{ try{ server=new ServerSocket(4331);
}
catch(IOException e1)
{ System.out.println("正在監聽"); //ServerSocket對象不能重復創建。
}
try{ you=server.accept();
System.out.println("客戶的地址:"+you.getInetAddress());
}
catch (IOException e)
{ System.out.println("正在等待客戶");
}
if(you!=null)
{ new Server_thread(you).start(); //為每個客戶啟動一個專門的線程。
}
else
{ continue;
}
}
}
}
class Server_thread extends Thread
{ Socket socket;Connection Con=null;Statement Stmt=null;
DataOutputStream out=null;DataInputStream in=null;int n=0;
String s=null;
Server_thread(Socket t)
{ socket=t;
try { in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{}
}
public void run()
{ while(true)
{ double a[]=new double[3] ;int i=0;
try{ s=in.readUTF();堵塞狀態,除非讀取到信息。
StringTokenizer fenxi=new StringTokenizer(s," ,");
while(fenxi.hasMoreTokens())
{ String temp=fenxi.nextToken();
try{ a[i]=Double.valueOf(temp).doubleValue();i++;
}
catch(NumberFormatException e)
{ out.writeUTF("請輸入數字字元");
}
}
double p=(a[0]+a[1]+a[2])/2.0;
out.writeUTF(" "+Math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2])));
sleep(2);
}
catch(InterruptedException e){}
catch (IOException e)
{ System.out.println("客戶離開");
break;
}
}
}
}
這些例子都是Java2實用教程上的.
❹ java socket傳送文件
客戶端代碼如下:
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.net.InetSocketAddress;
importjava.net.Socket;
/**
*文件發送客戶端主程序
*@authoradmin_Hzw
*
*/
publicclassBxClient{
/**
*程序main方法
*@paramargs
*@throwsIOException
*/
publicstaticvoidmain(String[]args)throwsIOException{
intlength=0;
doublesumL=0;
byte[]sendBytes=null;
Socketsocket=null;
DataOutputStreamdos=null;
FileInputStreamfis=null;
booleanbool=false;
try{
Filefile=newFile("D:/天啊.zip");//要傳輸的文件路徑
longl=file.length();
socket=newSocket();
socket.connect(newInetSocketAddress("127.0.0.1",48123));
dos=newDataOutputStream(socket.getOutputStream());
fis=newFileInputStream(file);
sendBytes=newbyte[1024];
while((length=fis.read(sendBytes,0,sendBytes.length))>0){
sumL+=length;
System.out.println("已傳輸:"+((sumL/l)*100)+"%");
dos.write(sendBytes,0,length);
dos.flush();
}
//雖然數據類型不同,但JAVA會自動轉換成相同數據類型後在做比較
if(sumL==l){
bool=true;
}
}catch(Exceptione){
System.out.println("客戶端文件傳輸異常");
bool=false;
e.printStackTrace();
}finally{
if(dos!=null)
dos.close();
if(fis!=null)
fis.close();
if(socket!=null)
socket.close();
}
System.out.println(bool?"成功":"失敗");
}
}
服務端代碼如下:
importjava.io.DataInputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.util.Random;
importcom.boxun.util.GetDate;
/**
*接收文件服務
*@authoradmin_Hzw
*
*/
publicclassBxServerSocket{
/**
*工程main方法
*@paramargs
*/
publicstaticvoidmain(String[]args){
try{
finalServerSocketserver=newServerSocket(48123);
Threadth=newThread(newRunnable(){
publicvoidrun(){
while(true){
try{
System.out.println("開始監聽...");
/*
*如果沒有訪問它會自動等待
*/
Socketsocket=server.accept();
System.out.println("有鏈接");
receiveFile(socket);
}catch(Exceptione){
System.out.println("伺服器異常");
e.printStackTrace();
}
}
}
});
th.run();//啟動線程運行
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidrun(){
}
/**
*接收文件方法
*@paramsocket
*@throwsIOException
*/
publicstaticvoidreceiveFile(Socketsocket)throwsIOException{
byte[]inputByte=null;
intlength=0;
DataInputStreamdis=null;
FileOutputStreamfos=null;
StringfilePath="D:/temp/"+GetDate.getDate()+"SJ"+newRandom().nextInt(10000)+".zip";
try{
try{
dis=newDataInputStream(socket.getInputStream());
Filef=newFile("D:/temp");
if(!f.exists()){
f.mkdir();
}
/*
*文件存儲位置
*/
fos=newFileOutputStream(newFile(filePath));
inputByte=newbyte[1024];
System.out.println("開始接收數據...");
while((length=dis.read(inputByte,0,inputByte.length))>0){
fos.write(inputByte,0,length);
fos.flush();
}
System.out.println("完成接收:"+filePath);
}finally{
if(fos!=null)
fos.close();
if(dis!=null)
dis.close();
if(socket!=null)
socket.close();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
❺ JAVA怎麼通過socket傳輸各種類型文件
IO
流的概念 什麼叫流?
管道 -
流的分類
位元組流 字元流
節點流 過濾流 (包裝流 處理流 功能流)
輸入流 輸出流
InputStream抽象類 所有位元組輸入流統一的父類
OutputStream抽象類 所有位元組輸出流統一的父類
FileInputStream節點流 能夠連接文件作為節點
int read() 無參read 一次讀取一個位元組 返回的就是位元組數據
int read(byte[] data)*
int read(byte[] data,int off,int len)
FileOutputStream
構造方法:File Stringboolean append
*:輸出流連接的文件會被自動創建出來 如果已存在那麼替換
*:但是輸出流連接的文件夾都不存在 直接Exception
write(int data)
write(byte[] data)
write(byte[] data,int off,int len)*
BufferedInputStream(節點輸入流[,緩沖空間大小])
BufferedOutputStream(節點輸出流[,緩沖空間大小])
*:它們是過濾流 包裝流 處理流 它們只能連接節點流 不能直接連文件
使用它們倆是為了提供緩沖空間 從而大幅度的提高每次讀寫的
吞吐量 從而提高效率
*:Buffered 意味著有緩沖空間 一定要清空緩沖區
flush();
close();
*:一定注意 你可以使用的依然是read方法來實現讀取 write 寫出
read() read(byte[] data,int off,int len)
write(int data) write(byte data,int off,int len)
DataInputStream
DataOutputStream
*:它們是過濾劉 包裝流 處理流 它們只能連接節點流 不能直接連文件
使用他們倆是為了提供讀寫基本數據類型內存原型的功能
readBoolean()readInt()readLong()
writeBoolean()writeInt()writeLong()
ObjectInputStream
ObjectOutputStream
*:它們是過濾劉 包裝流 處理流 它們只能連接節點流 不能直接連文件
使用他們倆是為了能夠提供對象持久化的功能
把Java當中不是基本數據類型的對象類型也能保存到文件中
implements Serializable
一個類型的對象如果想被持久化 那麼這個類型必須序列化
而且當中所有的屬性頁必須實現序列化介面
如果是個集合概念 那麼不但這個類型需要序列化
當中存放的任何一個元素都需要序列化
readObject()writeObject()
*:如果一個屬性不需要參與持久化 那麼可以直接把屬性定義
為 transient 短暫的 轉瞬即逝的
Reader抽象類 所有字元輸入流統一的父類
Writer抽象類 所有字元輸出流統一的父類
FileReader
int read() 無參read 一次讀取一個字元 返回的就是字元數據
int read(char[] data)
int read(char[] data,int off,int len)
FileWriter
*:輸出流連接的文件會被自動創建出來 如果已存在那麼替換
*:但是輸出流連接的文件夾都不存在 直接Exception
write(int data)
write(char[] data)
write(char[] data,int off,int len);
過濾流 包裝流 處理流
BufferedReader
String readLine() 一次讀取一行
字元串不能返回-1 返回null代表讀取結束
BufferedWriter
write(String str) 寫出字元串的方法
newLine() 寫出一個換行標識
PrintStreamSystem.out
PrintWriter
太有魅力了 太強大了
1.可以連接字元流也可以連接位元組流
2.可以當做節點流也可以當做過濾流
3.能夠指定自動清空緩沖 (流,true) autoFlush
4.可以指定字元集 字元編碼
5.一個println() = write()+newLine()
InputStreamReader
OutputStreamWriter
將原本的位元組流包裝轉換成字元流
RandomAccessFilesetLength();
Socket
伺服器 ServerSocket ss = new ServerSocket(7777);
while(true){
Socket s = ss.accept()
new XxxThread(s).start();
}
客戶端 Socket socket = new Socket(ip,port);
getInputStream();
getOutputStream();
❻ 用java socket實現一個伺服器對多個客戶端的文件傳輸
通過socket可以用如下方式進行。
1.啟動服務端代碼。
2.啟動客戶端自動連接服務端。
3.服務端上傳文件,保存文件和路徑。
4.將路徑發送給連接服務端的客戶端。
❼ 利用java socket實現文件傳輸
1.伺服器端
package sterning;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest {
int port = 8821;
void start() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
// 選擇進行傳輸的文件
String filePath = "D:\\lib.rar";
File fi = new File(filePath);
System.out.println("文件長度:" + (int) fi.length());
// public Socket accept() throws
// IOException偵聽並接受到此套接字的連接。此方法在進行連接之前一直阻塞。
s = ss.accept();
System.out.println("建立socket鏈接");
DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
dis.readByte();
DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream ps = new DataOutputStream(s.getOutputStream());
//將文件名及長度傳給客戶端。這里要真正適用所有平台,例如中文名的處理,還需要加工,具體可以參見Think In Java 4th里有現成的代碼。
ps.writeUTF(fi.getName());
ps.flush();
ps.writeLong((long) fi.length());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
// 注意關閉socket鏈接哦,不然客戶端會等待server的數據過來,
// 直到socket超時,導致數據不完整。
fis.close();
s.close();
System.out.println("文件傳輸完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String arg[]) {
new ServerTest().start();
}
}
2.socket的Util輔助類
package sterning;
import java.net.*;
import java.io.*;
public class ClientSocket {
private String ip;
private int port;
private Socket socket = null;
DataOutputStream out = null;
DataInputStream getMessageStream = null;
public ClientSocket(String ip, int port) {
this.ip = ip;
this.port = port;
}
/** *//**
* 創建socket連接
*
* @throws Exception
* exception
*/
public void CreateConnection() throws Exception {
try {
socket = new Socket(ip, port);
} catch (Exception e) {
e.printStackTrace();
if (socket != null)
socket.close();
throw e;
} finally {
}
}
public void sendMessage(String sendMessage) throws Exception {
try {
out = new DataOutputStream(socket.getOutputStream());
if (sendMessage.equals("Windows")) {
out.writeByte(0x1);
out.flush();
return;
}
if (sendMessage.equals("Unix")) {
out.writeByte(0x2);
out.flush();
return;
}
if (sendMessage.equals("Linux")) {
out.writeByte(0x3);
out.flush();
} else {
out.writeUTF(sendMessage);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
if (out != null)
out.close();
throw e;
} finally {
}
}
public DataInputStream getMessageStream() throws Exception {
try {
getMessageStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
return getMessageStream;
} catch (Exception e) {
e.printStackTrace();
if (getMessageStream != null)
getMessageStream.close();
throw e;
} finally {
}
}
public void shutDownConnection() {
try {
if (out != null)
out.close();
if (getMessageStream != null)
getMessageStream.close();
if (socket != null)
socket.close();
} catch (Exception e) {
}
}
}
3.客戶端
package sterning;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class ClientTest {
private ClientSocket cs = null;
private String ip = "localhost";// 設置成伺服器IP
private int port = 8821;
private String sendMessage = "Windwos";
public ClientTest() {
try {
if (createConnection()) {
sendMessage();
getMessage();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private boolean createConnection() {
cs = new ClientSocket(ip, port);
try {
cs.CreateConnection();
System.out.print("連接伺服器成功!" + "\n");
return true;
} catch (Exception e) {
System.out.print("連接伺服器失敗!" + "\n");
return false;
}
}
private void sendMessage() {
if (cs == null)
return;
try {
cs.sendMessage(sendMessage);
} catch (Exception e) {
System.out.print("發送消息失敗!" + "\n");
}
}
private void getMessage() {
if (cs == null)
return;
DataInputStream inputStream = null;
try {
inputStream = cs.getMessageStream();
} catch (Exception e) {
System.out.print("接收消息緩存錯誤\n");
return;
}
try {
//本地保存路徑,文件名會自動從伺服器端繼承而來。
String savePath = "E:\\";
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
int passedlen = 0;
long len=0;
savePath += inputStream.readUTF();
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));
len = inputStream.readLong();
System.out.println("文件的長度為:" + len + "\n");
System.out.println("開始接收文件!" + "\n");
while (true) {
int read = 0;
if (inputStream != null) {
read = inputStream.read(buf);
}
passedlen += read;
if (read == -1) {
break;
}
//下面進度條本為圖形界面的prograssBar做的,這里如果是打文件,可能會重復列印出一些相同的百分比
System.out.println("文件接收了" + (passedlen * 100/ len) + "%\n");
fileOut.write(buf, 0, read);
}
System.out.println("接收完成,文件存為" + savePath + "\n");
fileOut.close();
} catch (Exception e) {
System.out.println("接收消息錯誤" + "\n");
return;
}
}
public static void main(String arg[]) {
new ClientTest();
}
}
❽ java socket多文件傳輸問題
用多線程,每個線程創建一個socket連接,每個socket連接負責傳輸一個文件,服務端的serversocket每次accept一個socket連接,也建立一個新線程,該線程負責對應socket的文件傳輸
每個文件寫入完畢的時候關閉輸出流,建新文件後重新建立輸出流用於寫入
❾ java中怎麼用socket 一次傳多個文件啊
java中用socket一次傳多個文件,參考思路如下:
1、把線程放到Vector 線程池裡面;
2 、每次從Vector裡面拿到第一個空閑的,如果沒有,就新建一個線程,並保存到線程池, 線程狀態為使用中;
3 、線程完畢後,通知管理類,管理類把線程標識為空閑;
可以封裝為簡單的方法,如下:
public MyThread getFreeThread(){
.. // 從池裡面獲取一個空閑線程
}
public void finished(MyThread o){
// 線程通知管理類,我已經完成了
}