How can you create a function or component etc that will stop all running code until a condition is met?
For example the same way as a JOptionPane will do, if I have this for example:
JOptionPane.showInputDialog(null, "Hello", "Title", 1);
Within a function etc and then print to the console afterwards it it will not print until I close the JOptionPane.
I am guessing this component has some sort of thread setup built in to do this but how could I duplicate that with my own functions?
So say for example I wanted to make JFrames delay everything until it was closed so it acts like a JOptionPane.
Or for example have a function that had multiple inputs which got updated and inside it did some maths with those and if it was a certain value returned a boolean, but then everything else but those was paused until the true boolean was returned.
I am guessing the solution is some sort of thread setup but I am quite new to Java and when I have coded in the past I have not really used threads so I cannot create a good stop-start/pause-run style function system yet.
Does anyone have any suggestions how to achieve this or better yet code examples showing this type of thing working?
Have you tried making the thread sleep?
as simple as
Thread.sleep(timeInMilliseconds)
check here
You create a monitor (which is just a simple
Object
)Now you create a wait method
and a method to unlock your waiters.
So when you want to do something fancy, you can do it like this:
Of course, there are many possibilities, this is only one version of how to do it.