0

I have a Java app which acts like a command server for other devices and communicated with them via MQTT. I want to add a new functionality and don't let my sender class (implemented as a thread) send another message if the receiver class (also implemented as a thread) doesn't allow him to, let's say when a message containing "ACK" is received from a device.

I am trying to achieve it with wait and notify but no luck until now. How should I implement it?

I tried initializing the sender class inside the receiver so I can manipulate the thread but it keeps losing the connection when I notify the sender so he becomes running again.

I can upload the code if needed, thank you in advance.

asked Dec 20, 2017 at 13:30
3
  • I suggest you delete your question from this site and repost it to Stack Overflow, including the corresponding code. Implementation/coding help questions are off-topic here. Commented Dec 20, 2017 at 14:31
  • @RobertHarvey Isn't software engineering about software architecture too? Commented Dec 20, 2017 at 14:36
  • @GeorgeSp You're asking for a way to make one thread wait for another, correct? This is not really a question of software architecture and more about relaying tasks between threads. I would recommend you read up on LinkedBlockingQueue. When one thread is ready, it calls take() and when the other is ready, it calls put() allowing your threads to collaborate. Commented Dec 20, 2017 at 14:54

1 Answer 1

1

I found a solution to my problem.

I moved my class definitions to the file that main exists and declared a volatile variable which works like a mutex and synchronizes the two threads. Now it works like a charm.

In my opinion this was a architecture problem and not strictly programming related. I hope I help someone in the future with this.

answered Dec 20, 2017 at 15:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.