Writing Java Servers With GUI

I’ve only been programming Java for a week, but I’ve learned quite a bit in that time. Well, mostly from making a lot of mistakes. Generally, most mistakes are made during the learning process with a new language. Trying to build a damn GUI Application with a multithread proxy server was one of those mistakes.

Let me explain. I’m using Swing or more accurately a Swing Framework that comes with Netbeans, but not the Netbeans platform. One thing I’ve discovered through trial and error and then doing some researching, is that Swing only allows you one thread. Well, this isn’t completely accurately, but for all purposes, if you attempt to create a Thread and attempt to access any Swing object, your application is going to crash and burn. Also, at any point, your thread might run and deadlock the GUI thread causing your GUI application to become unresponsive.

So yeah, if you want to use Threads with Swing or really any GUI framework, except I think you might be able to get away with it if you are using OpenGL, but it is tough building a working GUI using OpenGL. I’ve came to the conclusion that I was doing it wrong half way through the entire process. The reason I didn’t scrap my current work was that I still didn’t have the proxy working correctly and I didn’t want to have to completely rewrite the entire application. I didn’t think I had the motivation to rewrite everything again and not even have a working product.

Well, I have both a working product and a GUI application. The solution was to use SwingWorker for the threads which basically adds the Runnable to Swing to run when it pleases and won’t lock the application. The problem remains is that I still need to create a thread to run two tasks side by side. A better description would be to say that I need to thread something that I don’t really care about when it finishes and appears to take forever to complete. It is currently slowing down the Proxy and there isn’t much I can do about it, unless I wanted to create another SwingWorker, but the problem is that I want to queue the messages sent to the thread and handle other tasks.

I want to create a Timer which will shut down the thread and close the connections if nothing had been received during that time. Currently there are stability issues with the GUI application not accepting any connections after the Server drops the connection or the application is closed.

What I thinking about doing is creating a new thread whenever a client socket is received, which will start a timer thread. There is currently a thread for the client to the server and another thread for the server to the client. What I’ll end up adding is another thread which will manage the messages sent to it and also eventually handle an OpenGL window application provided that it is allowed.

I’m thinking about keeping the GUI application. I can finish work on the separate process for the Proxy server and have the GUI manage the options and fork the Proxy server from the application. It would work almost exactly as it does now, but just work better and faster.

Possibly Related Posts:


Comments are closed.