Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Possible threading issue

  1. #1
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Possible threading issue

    Hi all.

    I have just joined, since I encountered a problem in a small personal project I have. To put it simple, the program is a very primitive web server that you can share files with (doesn´t host websites, it just shares stuff). It has a simple gui that helps you to, say, shut it down. You can also, or rather will be able to, read logs of what has happened and so on. And this is where my issue is.

    I have made my own logging system. It doesn´t involve any file IO, it will just list the different events in a window. So if someone connects and they, say, download a file it will appear in the gui automatically. Very simple, really, but there is a possible threading issue in it. Since the application is multithreaded, entries in the log will be added from many threads. Everytime an entry is added the gui will be updated, but from what I have read, this shouldn´t be done from several threads. So one idea is to simply use the SwingUtilities class to make so the gui is updated in its own thread. BUT this would also mean that if something is added while something is updated, the list may not present accurate data. Is this a real problem or just another case of a noob programmer complicating everything for himself ? Is there some way I can check if this is a real problem? And if it is a problem, what would the best way to deal with it be?

    Take care,
    Kerr.

    EDIT:

    In case it matters, there will be a limitation of how many entries the log will have at the same time. As I said, it is a small, personal project, so it won´t have the need to remember everything forever, and I do not want it to just use more and more memory... if that makes sense, lol.

    And the reason I am making my own logger rather then using the ones already there is because I want to learn how to make one .

    EDIT 2:

    Ok, I am not sure, but could I simply use the invokeAndWait method in SwingUtilities to prevent any threading issues when adding data to the log? And if I´m just confusing or something, please tell me. I fully realize I may not be that good at explaining things .
    Last edited by Kerr; March 6th, 2011 at 11:22 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

  3. #3
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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 .

  4. #4
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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 . 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.
    Last edited by Kerr; March 9th, 2011 at 05:02 AM.

Similar Threads

  1. java threading execution time question
    By centenial in forum Threads
    Replies: 4
    Last Post: September 8th, 2010, 11:32 PM
  2. problem in Threading !!!
    By roadies07 in forum Threads
    Replies: 4
    Last Post: July 14th, 2010, 10:21 AM
  3. Replies: 1
    Last Post: March 23rd, 2010, 02:29 AM
  4. Threading question....
    By neo_2010 in forum Threads
    Replies: 1
    Last Post: September 2nd, 2009, 02:38 AM
  5. Problem in Threading , ThreadGroup
    By neo_2010 in forum Threads
    Replies: 1
    Last Post: August 30th, 2009, 11:19 PM