Reputation: 6042
The easiest implementation is when we call from single class main method other classes implementing runnable:
public static void main(String [] args){
// declarations ...
receiver.start();
player.start();
}
Say inside receiver I have while loop which receives a packet value and I want to send that value to the second thread. How to do that?
Just to clarify I don't yet care about one thread controlling another, I just want first thread to share values with second.
And tiny question aside - does JDK 7 Fork really dramatically increases performance for java concurrent api?
Thank You For Your Time,
Upvotes: 4
Views: 370
Reputation: 16035
The java.util.concurrent -package contains many helpful interfaces and classes for safely communicating between threads. I'm not sure I understand your use-case here, but if your player
(producer) is supposed to pass tasks to the receiver
(consumer), you could for example use a BlockingQueue -implementation.
Upvotes: 1
Reputation: 403581
A simple option is to use a java.util.concurrent.atomic.AtomicReference
(or one of the other Atomic...
classes). Create a single instance of AtomicReference
, and pass it to the code that the various threads run, and set the value from the receiver thread. The other thread(s) can then read the value at their leisure, in a thread-safe manner.
does JDK 7 Fork really dramatically increases performance for java concurrent api?
No, it's just a new API to make some things easier. It's not there to make things faster.
Upvotes: 4