当前位置:编程学习 > JAVA >>

多线程socket数组对象,直接被NEW重写是否可行?


class TF extends Thread
{

public static SClient t[];
public static int maxthread=10;

public TF()
{
this.init();
}
public void init(){
t=new SClient[maxthread];
}

public void run()
{
int i =0;
for(i=0;i<t.length;i++){
t[i]=new SClient();
t[i].start();
System.out.println("thread-"+t[i].getId()+": building... ");
}

int dc[] = new int[maxthread];

while(true){
for(i=0;i<t.length;i++){
if(t[i].isAlive()==true && t[i].isExit()==false && t[i].isEnd()==false ){
dc[i]=0;
}else if(t[i].isAlive()==false){
dc[i]++;
System.out.println(" thread-"+t[i].getId()+": OffLine! Start trying again...  count="+dc[i]);
t[i]=new SClient();
t[i].start();
}
waiting(100);
}
waiting(1000);
}
}



public void waiting(Integer sec){
try{ 
TF.sleep(sec);
}catch(Exception e){
}
}

}



class SClient extends Thread {

private Socket sock = new Socket();
private InputStream in=null;
private OutputStream out=null;
    private InputStreamReader isr = null;
    private BufferedReader br = null;
private int port = 80;
private String host ="127.0.0.1";
private boolean _exit=false;
private boolean _end=false;


public void run(){
SocketAddress addr = new InetSocketAddress(this.host, this.port);
try{
sock.connect(addr, 3000);
this.in = sock.getInputStream();
this.out = sock.getOutputStream();
this.br = new BufferedReader(new InputStreamReader(this.in,"utf-8"));
System.out.println("thread-"+this.getId()+" connect ok");
}catch(Exception e){
this._exit=true;
System.out.println("thread-"+this.getId()+" connect error");
}
while(this._exit==false){
try{
System.out.println("thread-"+this.getId()+" "+(new Date() ) );
this.send();
}catch(IOException e){
this._exit=true;
System.out.println("thread-"+this.getId()+" IO error");
}catch(Exception e){
this._exit=true;
e.printStackTrace();
}
waiting(100);
}
System.out.println("thread-"+this.getId()+" close");
this.close();
this._end=true;
}

    public void close(){
        try {
        this.sock.close();
}catch(Exception e){
e.printStackTrace();
}
    }
    public void send() throws Exception,IOException {
        String msg = "send msg...end";
        this.out.write(msg.getBytes("UTF-8"));
        this.out.flush();
msg = null;
    }

public void waiting(Integer sec){
try{ 
SClient.sleep(sec);
}catch(Exception e){
}
}

public boolean isExit(){
return this._exit;
}

public boolean isEnd(){
return this._end;
}


}


新手,有几个问题还不是很确定,只好请教各位了。先谢过大家。

1 用 t=new SClient[maxthread]; 这样的方法实现进程数组是否有效率问题
2  t[i]=new SClient();
t[i].start();
这样用NEW 重写覆盖 线程数组中的对象是否有问题?会不会造成什么不太好的后果。
3 多次频繁的短的 out.flush(); 是否早曾瓶颈,如果对方服务器有连接和次数限制或者在网络稳定性和梵音哥不是很理想的时候,我在测试中本地一切正常,远程经过一段时间卡住。TF还会继续运行但 SClient 不知道为何会开住没了反映。








--------------------编程问答-------------------- --------------------编程问答-------------------- UPUPUPUPUP 顶一下。 大侠们给些意见 --------------------编程问答-------------------- --------------------编程问答-------------------- 1 用 t=new SClient[maxthread]; 这样的方法实现进程数组是否有效率问题
——没啥问题。


2 t[i]=new SClient();
t[i].start();
这样用NEW 重写覆盖 线程数组中的对象是否有问题?会不会造成什么不太好的后果。
——没啥问题。


3 多次频繁的短的 out.flush(); 是否早曾瓶颈,如果对方服务器有连接和次数限制或者在网络稳定性和梵音哥不是很理想的时候,我在测试中本地一切正常,远程经过一段时间卡住。TF还会继续运行但 SClient 不知道为何会开住没了反映。
—— flush()调用次数过多,也就是浪费点网络吞吐而已,也谈不上问题。
—— 目测第一个问题是,你建立了this.br = new BufferedReader(new InputStreamReader(this.in,"utf-8"));但其实你并没有读取任何内容,这种情况下如果服务端发送数据过来而一直没被读取掉(消费掉),服务器端的发送流会阻塞,进而可能导致服务器端也不会再接受客户端所发送的数据了。
—— 目测第二个小问题是,你没有在finally中去调用关闭socket,不过前面的异常你基本有捕
获,那么基本上只有小概率的RuntimeException可能导致问题;另外最好把流也关闭掉。
—— 另一个建议是不要清空dc[i]=0;这样你至少可以知道总体链接中断情况。

--------------------编程问答-------------------- --------------------编程问答-------------------- 在您对问题3的回复中:

—— 目测第一个问题是,你建立了this.br = new BufferedReader(new InputStreamReader(this.in,"utf-8"));但其实你并没有读取任何内容,这种情况下如果服务端发送数据过来而一直没被读取掉(消费掉),服务器端的发送流会阻塞,进而可能导致服务器端也不会再接受客户端所发送的数据了。

如果服务器因为发送阻塞导致接受也拒绝这的确是我没想到的一个问题。因为服务器返回的信息不是非常重要就没做考虑。但为避免这种阻塞发生的可能性。是否有设计单独的线程专门来负责接收服务器的返回信息?这方面您有什么好的思路么。 因SCLIENT被设计成了以发送为主的模式并没考虑到接受问题。 是给每个线程配对一套独立的接收线程还是每次发送前设置一个几毫秒的接收和超时抛异常? 

期待回复。


引用 4 楼  的回复:
1 用 t=new SClient[maxthread]; 这样的方法实现进程数组是否有效率问题
——没啥问题。


2 t[i]=new SClient();
t[i].start();
这样用NEW 重写覆盖 线程数组中的对象是否有问题?会不会造成什么不太好的后果。
——没啥问题。


3 多次频繁的短的 out.flush(); 是否早曾瓶颈,如果对方服务器有连接和次数……
--------------------编程问答-------------------- while循环看不出任何意义
如果是线程正常结束还有必要重启吗
一个建议,多线程里面不要用时间来干涉来判断线程的结束 --------------------编程问答--------------------
引用 6 楼  的回复:
在您对问题3的回复中:

—— 目测第一个问题是,你建立了this.br = new BufferedReader(new InputStreamReader(this.in,"utf-8"));但其实你并没有读取任何内容,这种情况下如果服务端发送数据过来而一直没被读取掉(消费掉),服务器端的发送流会阻塞,进而可能导致服务器端也不会再接受客户端所发送的数据了。

如果服务器因为发送阻塞导……

在客户端读就可以,while(br.readLine()!=null)
{

}
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,