I have a GUI (on its EDT thread) and another thread called "Recorder" created by the GUI's eventListener if a button is pressed:
if(actionEvent.getSource().equals(ui.record)) {
if(recorderThread == null) {
recorder = new Recorder();
recorderThread = new Thread(recorder);
recorderThread.start();
}
}
In the same event listener, I also implemented a mouseListener.
public void mouseReleased(MouseEvent mEvent) {
int x, y;
x = mEvent.getXOnScreen();
y = mEvent.getYOnScreen();
}
I want to pass these X and Y variables to my recorder object in my recorder thread when the mouse is clicked. I think I can bodge a solution with volatile variables, but I read somewhere that handlers can be used to pass information or invoke methods between two threads, and was interested in learning about it. I found this previous post that faced a similar problem to mine.
The solution to the post, however, quite confused me. I think the person passes the thread objects into the handler, that way any thread can just call all the objects inside that handler? For example:
handler(someObj);
then in another thread
handler.getSomeObj().methodInObj();
But I'm not entirely sure if that is how handlers work. In addition, they also seem to be dealing with Swing's background thread instead of a separate thread the user creates(If this is the same concept, apologies in advance).
Finally, the solution seems to have called a Handler class built into the Java library, whereas I want to write my own handler class so I can better learn how threads communicate (since I'm a really novice youtube taught programmer). If anyone can help me out, it'd be greatly appreciated. Thanks in advance!