0

我目前正在开发一个基于多线程套接字的 Java 程序,该程序应该允许多个线程向该程序发送请求。这应该通过事件激活来处理,但我很难理解事件及其实现。下面是应该允许超过 1 个线程与程序通信的代码,但我那里只有 1 个线程。有人可以对此进行更多说明吗?非常感激。

//this is a a threads class 
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Niti implements Runnable {

    public String line;
    public Socket soc;
    public boolean active=true;

    public Niti(Socket soc)
    {
        this.soc=soc;
        this.line=line;
        this.active=active;
    }


    public synchronized void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(soc.getInputStream()));
            line=br.readLine();
            while(line!=null && !line.equals("")){

                if(!this.active)
                   break;

                System.out.println(line);
                line=br.readLine();
            }

            BufferedOutputStream bos = new BufferedOutputStream(soc.getOutputStream());
            bos.write("Poruka iz Programa".getBytes());

        }
        catch (IOException ex) {
            Logger.getLogger(Niti.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {        
           soc.close(); 

        } catch (IOException ex) {
            Logger.getLogger(Niti.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}



import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


//and this is the main class
public class Server{


    public static synchronized void main(String[] args) throws IOException, InterruptedException{

        ServerSocket ss = new ServerSocket(1000);

        while(true){
            Socket sokit = ss.accept();
            Niti n = new Niti(sokit);
            while(true){

                Thread t = new Thread(n);
                t.start();

                //Thread.sleep(4000);
                //n.active=false;
                System.out.println("nit broj:" + Thread.currentThread().getId()); 
            }   
        }
    }
}
4

1 回答 1

0

好吧,如果不看 Niti 类(我想那是客户端处理程序类),你会在这里遇到一个逻辑错误:

    while(true){
        Socket sokit = ss.accept();
        Niti n = new Niti(sokit);
        while(true){ // LOGIC ERROR!!!

            Thread t = new Thread(n);
            t.start();

            //Thread.sleep(4000);
            //n.active=false;
            System.out.println("nit broj:" + Thread.currentThread().getId()); 
        }   
    }

使用上面的代码,您在第一次通过 accept 方法后创建无限线程。您需要做的是删除第二个 while(true),如下所示:

    while(true){
        Socket sokit = ss.accept();
        Niti n = new Niti(sokit);

        Thread t = new Thread(n);
        t.start();

        //Thread.sleep(4000);
        //n.active=false;
        System.out.println("nit broj:" + Thread.currentThread().getId());               
    }
于 2013-04-11T19:33:02.630 回答