Re: Possible threading issue
While I'm not fully understanding the design, it seems you are having multiple threads that may update a GUI. A few points. 1) If you are update a JTextCopmonent, the setText method is thread safe (see JTextComponent (Java Platform SE 6)). Next, using SwingUtilities will throw everything onto a single thread and probably accomplish what you want 3) If you want a more comprehensive and low level method (that is independent of the user interface), create a single thread (that all other threads who wish to log have access to) which acts as a queue to which you can add your logging statements. The run implementation of this thread can then wait on the queue, and when it is not empty remove the first element and do its thing. This implementation pushes all logging onto a single thread (of course, if you are updating Swing components then use SwingUtilities to make sure you are updating them on the EDT)
Re: Possible threading issue
Well, my design is simple. I have a class, Logger, that I use to log stuff. When something happens, logger will simply "publish" the event to a number of listeners it has. The observer pattern, basically (I think, lol). So far no GUI is involved. The GUI part is implemented as a listener, that also acts as a model for the JList that will list the different events. So when it receives a log entry, it will store it in an internal list and then update the GUI. And this is where the possible threading issues come in. From what I have read, and experienced a bit as well, it should be avoided to update the GUI from multiple threads. It is better to have the GUI running in its own thread and have that thread do all the handy work. Read an explanation of why, which I at the moment have forgot, but apparently multithreading and GUI doesn´t mix very well, so I generally try to avoid it.
In this case, the problem is this, however. If I have a maximum of 5 log entries, and one gets added, then the log entries will have to be moved. If I at the same time update it in the event dispatch thread, or whatever the name is, then the data may be inaccurate, since suddenly stuff changes position in the log list while updating the GUI. It is all very theoretical, of course, and I think that the invokeAndWait approach will solve the problem (the listener that I use to update the GUI is thread-safe, after all) since it will mean the log will not be updated at the same time as the GUI is... if I have understood how the method works correctly, at last.
Have thought of something similar to option number 3, but I felt it wasn´t needed in this case, and decided to try a more simple approach. I don´t mind getting my hands dirty and doing stuff like that, though, it can be fun :).
Re: Possible threading issue
Lol, now of course I managed to make so the program will crash if I have the log view gui thing up, then close it, then brings it up again and try and surf on the web server :-bd. No exception if thrown, it just freezes... anyone have an idea of how to figure out where the problem is?
EDIT:
Ok, got it to work by moving thread safety from the listener to the logger.