Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inter-threads communication

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,

like image 886
Aubergine Avatar asked Nov 16 '25 02:11

Aubergine


1 Answers

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.

like image 187
skaffman Avatar answered Nov 17 '25 17:11

skaffman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!