创建一个使用线程池等待来自客户端的消息的Java服务器套接字

问题描述 投票:0回答:1
@FXML
private TextArea textarea;
@FXML
private ImageView imagev;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    Serverth Server = new Serverth();
    Server.start();
}


class Serverth extends Thread {
    @Override
    public void run() {
        try {
            final int NUM_THREAD = 99;


            ServerSocket socket = new ServerSocket(8078);
            ExecutorService exec = Executors.newFixedThreadPool(NUM_THREAD);
            System.out.println("SERVER SOCKET CREATED");


            while (!isInterrupted()) {
                Socket in = socket.accept();
                Runnable r = new ThreadedHandler(in);
                exec.execute(r);

            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

    }
}

class ThreadedHandler implements Runnable {

    private Socket incoming;

    public ThreadedHandler(Socket in) {
        incoming = in;
    }

    public void run() {
        try {
            try {
                ObjectInputStream is=new ObjectInputStream(incoming.getInputStream());

                while(true) {
                    if (is.available() > 0) {
                        String line = is.readUTF();

                        textarea.appendText("\n" + "[" + new java.util.Date() + "] : " + line);

                        if (line.contains("inviato")) {
                            Object obj = is.readObject();
                            Email ema = (Email) obj;
                            try {
                                SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy-hh-mm-ss");
                                FileOutputStream fileOut = new FileOutputStream("src/Server/" + ((Email) obj).getDestinat() + "/" +  formatter.format(((Email) obj).getData()) + ".txt");
                                ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
                                objectOut.writeObject(ema);
                                objectOut.flush();
                                objectOut.close();
                                System.out.println("The Object  was succesfully written to a file");

                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }

                        }

                    }
                }
                } catch(IOException ex) {
                ex.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
            try {
                incoming.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
    }
}

在run方法(在Serveth类中)中,我创建了一个服务器套接字并调用exec.execute方法。在run方法(在ThreadedHandler类中)中,服务器正在等待来自客户端的消息(在此特定情况下,它会创建一个新的.txt文件,但这并不重要)。一切正常,但会导致过度使用CPU和延迟!!!>

@ FXML私有TextArea textarea; @FXML私人ImageView imagev; @Override public void initialize(URL url,ResourceBundle resourceBundle){Serverth Server = new Serverth(); Server.start(); } ...

java multithreading sockets message runnable
1个回答
1
投票

InputSteam.available方法立即返回一个值,告诉您没有字节可读取,因此此代码运行一个非常“热”的自旋循环:

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.