有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程如何在Java中创建事件

我目前正在开发一个基于多线程socket的Java程序,该程序应该允许多个线程向该程序发送请求。这应该通过事件激活来处理,但我很难理解事件及其实现。下面是允许多个线程与程序通信的代码,但我只有一个线程。有人能解释一下吗?非常感谢

//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()); 
            }   
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    好吧,如果没有look to 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());               
        }