V
V
Vadim Gush2014-09-17 16:40:18
Java
Vadim Gush, 2014-09-17 16:40:18

How to make two threads in Java?

I can't solve the problem with the server. It is necessary that Input and Output be in different streams, but how to separate them into two different streams? I tried, but after running them I got this:

Exception in thread "Thread-1" Exception in thread "Thread-0" java.lang.NullPointerException
  at server.ServerInput.run(ServerInput.java:21)
  at java.lang.Thread.run(Thread.java:745)
java.lang.NullPointerException
  at server.ServerOutput.run(ServerOutput.java:20)
  at java.lang.Thread.run(Thread.java:745)

Here is the code that I need to split into two threads. Help me please:
try {
      server = new ServerSocket(SettingsServer.getPort(), SettingsServer.getMaxUsers());
      while (true) {
        connect = server.accept();
        output = new ObjectOutputStream(connect.getOutputStream());
        input = new ObjectInputStream(connect.getInputStream());
        
        inObject = (String)input.readObject();
        
        if (inObject.equals("<html><table><tr><td>&&&$server_recd;</td></tr></center></html>") == true) {
          SettingsServer.CounterPlus();
        }
        
        if (inObject.equals("<html><table><tr><td>/server</td></tr></center></html>") == true) {
          SettingsServer.CounterPlus();
          sendData(SettingsServer.getInformation());
        }
        
        if (inObject != null && inObject.equals("<html><table><tr><td>&&&$server_recd;</td></tr></center></html>") == false && inObject.equals("<html><table><tr><td>/server</td></tr></center></html>") == false) {
          SettingsServer.CounterPlus();
          last_message.setText(inObject);
          sendData("&&&$client_recd;");
        }
        
        inObject = null;
        
      }
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (HeadlessException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2014-09-18
@zolt85

What practical problem do you want to solve?

E
ensiriuswOw, 2014-09-18
@ensiriuswOw

Bad programming style:
Good programming style:
The code looks very doubtful. What are you trying to do? I advise you to look towards Netty if performance is important to you. You can't tell from your code what the problem is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question