Is it possible to control polling intervals with GLFW? - java

I have a slight stutter in my 3D openGL application which I think can be resolved by reducing the amount of keyboard input that is processed. So, I was wondering if it is possible to set an interval on glfwPollEvents or somehow control when/how often glfwSetKeyCallback accepts key input? (eg. user holds the key down but I only want to process it every x millisecs). I have tried using a Timer object but that results in an illegalmonitorstate exception.

Related

Applying window based rules in Apache Flink Broadcast stream

I have a set of rules in my BroadcastStream in Apache Flink.
I am able to apply new rules as they come to my stream of events.
But I am not able to figure out how can I implement if my rules are like
rule 1> alert when count of event a is greater than 5 in a window of 5 mins
rule 2> alert when count of event a is greater than 4 in a window of 15 mins
I am a newbie to flink. I am not able to figure this out.
An application based on flink-sql or flink-cep won't be able to do this, because those libraries can only handle rules that are defined at the time the job is compiled. You would need to start a new job for each new rule, which may not meet your requirements.
If you want to have a single job that can handle a dynamic set of rules that are supplied while the job is running, you'll have to build this yourself. You can use a KeyedBroadcastProcessFunction to do this (which it sounds like you have already begun to experiment with).
Here's a sketch of a possible implementation:
You can use keyed state in the KeyedBroadcastProcessFunction to keep track of the current count in each window. If the rules can be characterized by a time interval and a counting threshold, then you could use MapState, where the keys are the rule IDs, and the values in the map are the current count for that rule. You can have a timer for each rule that fires when each window ends.
As events arrive, you iterate through the rule-based map, incrementing the counter for every relevant rule. And when the timers fire, you find the relevant rules, compare the counters to the thresholds, take appropriate action, and clear those counters.
Some potential complications to keep in mind:
This implementation requires that you partition your stream with a keyBy, so that you can use MapState and timers.
The broadcast stream can't have timers associated with it, so the timers will have to be managed by the processElement method that's handling the keyed stream.
Flink only allows one timer for a given key and given timestamp. So take care if you must handle the case where two rules would need to be triggered at the same time.
If events can arrive out of order, then you will need to either first sort the stream by timestamp, or allow for having multiple windows open concurrently.

Is drawing straight lines in paint using SHIFT and mouse dragging an example of multithreading?

I am studying multi-threading and now I am stuck. I read that Multi-threading in java is a process of executing multiple threads simultaneously. What does this 'simultaneously' mean? Also, multi-threading is based upon the concept of time-sharing, so how can the threads execute simultaneously? And if threads can't execute simultaneously, then how are we able to draw straight lines in paint by holding the shift key and dragging the mouse at the same time?
A short overview:
Indeed most Threads do not run at the same time. The trick is that threads switch often between one another. This gives the illusion of parallelism. (Similar to how images in quick succession give the illusion of continuous motion, quickly changing threads give the illusion of running at the same time)
How paint is implemented is anyone's guess. Here goes mine:
You press shift, a boolean flag gets raised
You move your mouse, the line is calculated
If you raise the shift key the bool flag is switched, causing the line not to be straight anymore
Ideed, key listening is an action that often is multithreaded. You have a thread that just asks all keys every few milliseconds if they were pressed. If so it sends a message to other treads (if they are listening for that particular key). The same goes for reading mouse movement.
Usually, you do not have to go in great detail for IO, as it is handled by most librarys/ frameworks for you. Also IO requires OS support, so that opens a new can of worms
I hope this helped
Processor can't execute several threads at one time, so threads are executing pseudo-simultaneously.
At this picture each color means each thread. Processor execute one thread then second etc. It seems like they are executing sumultaneously, but probably processor just change current thread and execute this.

I want to know the difference between cvWaitKey(0) and cvWaitKey(10)

cvShowImage("SMART", cropped);
cvWaitKey(10);
cvShowImage("SMART", cropped);
cvWaitKey(0);
What is the difference between these two functions and when i use this in infinite for loop cvWaitKey(10) works but cvWaitKey(0) or cvWaitKey(30) do not work. What is the reason?
The difference can be found in the documentation of OpenCV of the waitKey function.
Basically, the function waitKey waits for a key to be pressed, and the argument is the amount of time it will wait. So, when you use 10. It will wait 10 milliseconds and then continue with the program.
The documentation says:
0 is the special value that means “forever”
So, when you use 0. The program will wait for key to be pressed forever... Just pressing any key will continue the program... (also closing the window will do it)
I recommend to use 10 when you are doing a stream of pictures (maybe from a camera). And use 0, when you are expecting human interaction for the program to continue. And a bigger value if you want to see it for enough time, but continue the program without any interaction.

Making Java game, found easy way to cheat, don't know how to prevent it

I'm making a game in Java, and I made it so that if you right click, the player teleports to the mouse to "escape". I want to make it so that you can only use it every 2 mins. and after trying and failing THAT, I found out that you can just hold down right mouse and the player will follow your mouse/clicker. I am using Processing 3.1.2 if that helps at all.
Every time you allow that player power to be used, note the current timestamp.
Next time the player attempts to activate that power, check the saved timestamp against the current time. If an insufficient number of seconds have passed, disallow the power.
If sufficient time has passed and you allow the power to activate, update the variable holding the time that the power was last used.
This is often called a "cool down" in games.
I would suggest using a javax.swing.timer. I have done this before, and within the mouseClicked event you set a boolean canTeleport = false. At the end of the javax.swing.timer, set canTeleport = true. The first thing that you can do when going inside mouseClicked,
if(canTeleport)
{
//teleport
}
//start timer

Can I use the Java timer class for timed data collection?

I am writing a program that collects a series of angle inputs during a trial and saves these to a file with their corresponding timestamps.
For example:
1 sec 260 degrees
2 sec 45 degrees
3 sec 60 degrees
etc.
When running a trial, the user should be able to pause and restart or fully abort the trial if need be.
I am running into trouble with the pause functionality. I have been using the Java Timer and TimerTask classes to time the input, as it provides much of the functionality I am looking for (start a task after a delay, only record data at certain intervals, etc.).
Within my timerTask, I have been storing the collected angles and times in parallel arrays, and then at the end of the trial, writing these arrays to a file.
However, when I "pause" my timerTask via the timer.cancel() function and restart it, the old data arrays are thrown away. I have tried to sidestep this issue by saving the "paused" array and then merging it with the "restarted" array at the end of the restarted trial, but this doesn't account for the fact that a trial could be paused numerous times.
Are the timer/timerTask classes the wrong classes to be using for this job? Is there a better way to collect time-based data in Java? Or am I just overlooking a solution?
As the API specifies Timer.cancel()
Terminates this timer, discarding any currently scheduled tasks.
The simplest way to achieve the functionality you desire would probably be to store a 'paused' boolean and toggle it when the user pauses/unpauses. Then check the state of the boolean from within your task and simply return; if the trial is paused. The Timer will still fire every second, but nothing will happen as long as the trial is paused.

Categories

Resources