Results 1 to 5 of 5
  1. #1
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default Timers, Networking, Threads Error

    Hey guys,
    I have just realised what hole I have dug myself into.
    I keep getting BufferOverflowException errors randomly when reading/writing ByteBuffers. I have narrowed it down to TimerTasks...

    I am using an NIO system where all reading, writing is happening in one thread for all clients. The problem is that I am using TimerTasks (running on separate threads obviously) to do things like Ping clients every 5 seconds.

    Every so often this will occur at the same time I am reading/writing (usually larger chunks of data) and cause the BufferOverflowException error.

    Is there an efficient way of calculating the time or an alternative way of working this?

    Also I have a while(true){} loop thats runs through reading/writing functions, it also contains a Thread.sleep(1); which I know is bad practice, but using a timer here would just exasperate the problem. What is the way round it?

    Thank you for reading and your help,
    Dan

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Dan0100 View Post
    I am using an NIO system where all reading, writing is happening in one thread for all clients. The problem is that I am using TimerTasks (running on separate threads obviously) to do things like Ping clients every 5 seconds.

    Every so often this will occur at the same time I am reading/writing (usually larger chunks of data) and cause the BufferOverflowException error.
    You could set a variable to true when writing to a file and false when it isn't, then have the ping code check its value. I'm still not quite sure how pinging and writing to a file is causing a buffer overflow, though. Something must be getting mixed up...

  3. #3
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok if I use a true / false variable e.g. a lock how can I make sure, even if it IS locked that it will execute the code that locked it first and then the sending code?

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Well if you disallow the ping while it is locked, it will simply not do that ping. What you could do is, at the end of the I/O function, ping the user automatically regardless of the ping timer.

    Example:
    Java Code:
    // ------------------------- Ping thread
    public static boolean locked = false;
    public void PingTimer() {
        if (locked) return;
        // Timer to call PingUsers() every 5 seconds
    }
    public void PingUsers() {
        // Code to ping the users
    }
    
    // ------------------------- Other thread
    public void FileIO() {
        PingClass.locked = true;
        // Your File IO
        PingClass.locked = false;
        PingClass.PingUsers();
    }
    Last edited by Zack; 09-22-2010 at 11:18 PM.

  5. #5
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks for the replies Zack! I had already tried using a locking boolean but I found that it was a bit crazy for me to implement with the number of functions I had going.

    I HAVE found a solution, and it is very simple.

    My main thread loops through the reading/writing functions and pauses for 1ms.
    My TimerTask now sets a doPing boolean to true (instead of calling the ping function).
    My main thread then reads this doPing boolean, if true it will write the ping output and set the boolean to false.

    This has many advantages:
    - only 1 variable is needed
    - only 1 variable check is needed
    - the timerTask doesn't need to worry about the function being called
    - ***the functions can be called in a specific order***

    Thanks again for your help Zack. I hope this helps some other people :)

Similar Threads

  1. I need help with timers
    By Chris Rice in forum New To Java
    Replies: 15
    Last Post: 09-04-2013, 07:02 PM
  2. Job scheduling using timers in java
    By umapathy_sekar in forum New To Java
    Replies: 1
    Last Post: 09-13-2010, 05:18 PM
  3. Help - Swing Timers, Two Keypresses
    By Gheta in forum AWT / Swing
    Replies: 2
    Last Post: 07-29-2009, 09:23 PM
  4. Threads and Timeout in Socket Networking
    By byuu in forum Threads and Synchronization
    Replies: 2
    Last Post: 05-27-2008, 09:05 PM
  5. help with networking and threads
    By byuu in forum Networking
    Replies: 9
    Last Post: 05-21-2008, 05:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •