The issue of multithreading and Java Swing

Hi, I have a GUI application that works fine. I created a socket server. When I create a new Server class object in the program, the GUI application stops responding.

This is my server class. If i do

Server s = new Server();

in my main application it stops working. How to add it? Creating a new topic? I tried

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

but the problem continued. Please, I will be grateful for your help.

package proj4;

import java.net.*; 
import java.io.*; 

public class Server implements Runnable { 
    ServerSocket       serverSocket = null;
    Socket             clientSocket = null;
    ObjectOutputStream out          = null;
    ObjectInputStream  in           = null;
    int                port;
    static int         defaultPort  = 30000;
    boolean            isConnected  = false;
    Thread             thread;
    DataPacket         packet       = null;

    public Server(int _port) {
        try {
            serverSocket = new ServerSocket(_port);
            serverSocket.setSoTimeout(1000*120);  //2 minutes time out     
            isConnected = true;
            System.out.println("server started successfully");
            thread = new Thread(this);
            thread.setDaemon(true);
            //thread.run();
        } catch (IOException e) {
            System.err.print("Could not listen on port: " + port);
            System.exit(1);
        }
        try {
            System.out.println("Waiting for Client");
            clientSocket = serverSocket.accept();
            System.out.println("Client Connected");
            thread.run();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }
        try {
            out = new ObjectOutputStream(clientSocket.getOutputStream());
            System.out.println("output stream created successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            in = new ObjectInputStream(clientSocket.getInputStream());
            System.out.println("input stream created successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Server() {
        this(defaultPort); //server listens to port 30000 as default
    }

    public void run() {
        System.out.println("Thread running, listening for clients");//debugging purposes
        while (isConnected) {
            try {
                packet = this.getData();
                Thread.sleep(0);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    } 

    public DataPacket getData() {
        try {
            packet = (DataPacket)in.readObject();
        } catch (Exception ex)  {
            System.out.println(ex.getMessage());
        }
        return packet;
    }

    public void sendData(DataPacket dp) {
        try {
            out.writeObject(dp);
        } catch (IOException e) {
            e.printStackTrace();
        } 
        try {
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void closeConnection() throws IOException {
        out.close(); 
        in.close(); 
        clientSocket.close(); 
        serverSocket.close(); 
    }
} 
+3
source share
5 answers

Constructor block Server, potentially undefined, c accept().

Two things about Swing programs:

  • Never do a long task in a Swing event stream and
  • Swing Swing, .

, Swing, , , , Server. , Swing .

, , ? , Thread.start(), run(), , " " .

:

  • , Thread.sleep(0); run(). . CPU, -op, .
  • , isConnected volatile - , , , .
  • isConnected false, , run() , JVM RuntimeException.
  • Threads . (. Java Concurrency .)
  • accept ServerSocket, Thread run()! !
  • :

:

thread = new Thread(this);
thread.setDaemon(true);
//thread.run();

thread.run(), not, Thread! Thread.start(). Thread ( № 3 ) , . , IOExceptions , . , isConnected false IOException, closeConnection().

+11

, . Cosntructor ( ). run() , run().

, Thread() - , , , - .

+4

( , .) , , GUI . , , , - .

,

(new Thread(new Server()).start();

, .

: , , , ,

(new Thread(new Server()).start();
System.err.println("Got here!");

, "!". . , GUI.

+1

SwingWorker, Java 6. Java, :

https://swingworker.dev.java.net/

SwingWorker - , , . :

http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

ServerSwingWorker .

0

,

    new Thread(new Server())

, " ()" .

- :

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            new Server();
        }
    });
    t.start();
0
source

Source: https://habr.com/ru/post/1705951/


All Articles