Results 1 to 5 of 5
- 09-22-2010, 03:21 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 18
- Rep Power
- 0
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
- 09-22-2010, 03:33 AM #2
- 09-22-2010, 10:41 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 18
- Rep Power
- 0
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?
- 09-22-2010, 11:15 PM #4
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.
- 09-22-2010, 11:28 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 18
- Rep Power
- 0
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
-
Job scheduling using timers in java
By umapathy_sekar in forum New To JavaReplies: 1Last Post: 09-13-2010, 05:18 PM -
I need help with timers
By Chris Rice in forum New To JavaReplies: 14Last Post: 03-07-2010, 09:04 PM -
Help - Swing Timers, Two Keypresses
By Gheta in forum AWT / SwingReplies: 2Last Post: 07-29-2009, 09:23 PM -
Threads and Timeout in Socket Networking
By byuu in forum Threads and SynchronizationReplies: 2Last Post: 05-27-2008, 09:05 PM -
help with networking and threads
By byuu in forum NetworkingReplies: 9Last Post: 05-21-2008, 05:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks