Posted 04 July 2013 - 02:06 AM
In short, a function that states how many messages are actually waiting in the event queue.
It would greatly help with event management and keeping the flow of programs.
The current way things are, if there are no items in the event queue and a program yields, it sits there waiting for input.
If a program (or thread) is supposed to be handling an automated task and something such as this comes around, the program could end up stuck without input.
The two usual solutions to this are to queue an empty event or queue a timer. Each of these methods have problems however:
Firstly, the empty event is an easy way to clog up the system. Whilst queueing an empty to ensure an instant return from yield can manage a lot of issues, it creates the new issue of clogging up the event queue with empty messages, one for every actual event that comes in.
The issue with the timer method is that it forces the program to handle multiple events at once, and if some of those events happen to be ones it doesn't handle, the program must then re-queue the events in the same order they arrived, which can be awkward if more events are added after the timer fires or if the event the program wants is right near the end.
Adding the ability to check how many events are in the queue or similar pre-emptive functions would make event handling much easier and could enhance the current methods of event handling to make programs run much more smoothly and make event handling systems much less clunky and verbose.
For example if a program wants to check for events but ensure continuous execution (ie not having to explicitly wait for input), it could simply check if events are in the queue, if there are it handles them, but if not it uses the empty event trick to force its continued execution. The resultant code compared to current systems would be much more condensed and prevent issues such as flow interrupt and clogging up the event queue. Furthermore, if the event queue system is implemented using an actual queue, it should just be a matter of setting up a function to return the queue's size.
An alternative would be the ability to peek the queue, which would achieve a similar effect. Again, assuming event queues are implemented using actual Java queues, they should natively support both size checking and peeking.
It would greatly help with event management and keeping the flow of programs.
The current way things are, if there are no items in the event queue and a program yields, it sits there waiting for input.
If a program (or thread) is supposed to be handling an automated task and something such as this comes around, the program could end up stuck without input.
The two usual solutions to this are to queue an empty event or queue a timer. Each of these methods have problems however:
Firstly, the empty event is an easy way to clog up the system. Whilst queueing an empty to ensure an instant return from yield can manage a lot of issues, it creates the new issue of clogging up the event queue with empty messages, one for every actual event that comes in.
The issue with the timer method is that it forces the program to handle multiple events at once, and if some of those events happen to be ones it doesn't handle, the program must then re-queue the events in the same order they arrived, which can be awkward if more events are added after the timer fires or if the event the program wants is right near the end.
Adding the ability to check how many events are in the queue or similar pre-emptive functions would make event handling much easier and could enhance the current methods of event handling to make programs run much more smoothly and make event handling systems much less clunky and verbose.
For example if a program wants to check for events but ensure continuous execution (ie not having to explicitly wait for input), it could simply check if events are in the queue, if there are it handles them, but if not it uses the empty event trick to force its continued execution. The resultant code compared to current systems would be much more condensed and prevent issues such as flow interrupt and clogging up the event queue. Furthermore, if the event queue system is implemented using an actual queue, it should just be a matter of setting up a function to return the queue's size.
An alternative would be the ability to peek the queue, which would achieve a similar effect. Again, assuming event queues are implemented using actual Java queues, they should natively support both size checking and peeking.