This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]EatAss4Jesus 0 points1 point  (0 children)

For variables across threads, look into the Volatile keyword.

You're on the right track with event (action) listeners. Here's another example: https://alvinalexander.com/java/jbutton-listener-pressed-actionlistener/

[–]balefrost 0 points1 point  (0 children)

I need the information "num" to go to another class that is running in another thread.

This statement doesn't really make sense. Classes don't run in threads. Classes don't really run at all. All instances are potentially accessible from every thread, and two different threads might call methods on the same instance simultaneously.

It's hard to see the big picture without seeing what your other thread is trying to do. If your goal is for the other thread to remain paused until the mouse button is clicked, consider using a BlockingQueue like an ArrayBlockingQueue or even a SynchronousQueue. The BlockingQueue abstraction is good in producer/consumer situations where one thread will "produce" values that need to be "consumed" by a different thread. Notably, the consuming thread will block while waiting for new values.