aspose file tools*
The moose likes Threads and Synchronization and the fly likes Multi-Thread communication. if not data from thread -> time out. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multi-Thread communication. if not data from thread -> time out." Watch "Multi-Thread communication. if not data from thread -> time out." New topic
Author

Multi-Thread communication. if not data from thread -> time out.

Ibrahim Gapuraev
Greenhorn

Joined: Dec 13, 2013
Posts: 5
Hi everyone,
I will try to explain myself as clear as possible. I think the problem that I am experiencing now, is just because of luck of knowledge about multi-threading.
I have to simulate work of heart and pacemaker. Pacemaker senses hearts atrium beat. If there is no atrium beat for a short time, say, 2 ms, pacemaker will initiate artificial pace.

Design of app.
I have class hart, which is Runnable. In method run(), there is simulation of heart. It produces beat, if successful, then send information to Pacemaker thread through SynchronousQueue, that atrium beat has been initiated, else - no action form heart. It also produces beats for ventricle of heart, but this part of heart is not faulty in this scenario. Pacemaker should realise that there is no beat form ATRIUM of heart and initiate beat.

Code for Heart.java
private fields, constructor, mutator methods...





[javadoc] /**
* Method of pacemaker, that causes heart to do a beat.
* @param the heart - heart that pacemaker is operating upon.
*/
[/javadoc]


This code produces double beats (atrium + delay ~2 ms, + ventricle beat).

Now, code for Pacemaker:

This is my question:
How to do something like this code attempts to do?
How can I know that heart did not send any signal?
It should run visually correct as well, visual print statements should appear like this:
beat -> delay 0.2 sec -> second beat ->delay 1 sec ->repeat.




Really sorry if this is not clear enough. I will add any info that you need. I am struggling with it second day. Please help me good people
Thank you.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2434
    
  28

You might want to think about seperating your thread from your data, and it might make more sense. For example, you can have the Heartbeat class that represents when the heartbeats have occured. Your heart class should start a thread and have a reference to the heartbeat class. The heart thread can wake up every so often and update the heartbeat class. The pacemaker class also has a reference to the same heartbeat and can wake up every so often and check when the last heartbeat happenned

Also, go step by step. DOn;t try to write out all the code at one. First implement your heartbeat, then implement heart that updates heartbeat without any randomization. Then implement Pacemaker and all it does is check the heartbeat without trying to apply any trigger, just log a message that it got a heartbeat. Then add randomization to the heart and see if pacemaker detects missed heartbeats, and then finish your pacemaker class


This is how I would do it. There are other ways to do it. You could have a queue between heart and pacemaker, for instance.
Ibrahim Gapuraev
Greenhorn

Joined: Dec 13, 2013
Posts: 5
Jayesh A Lalwani wrote:You might want to think about seperating your thread from your data, and it might make more sense. For example, you can have the Heartbeat class that represents when the heartbeats have occured. Your heart class should start a thread and have a reference to the heartbeat class. The heart thread can wake up every so often and update the heartbeat class. The pacemaker class also has a reference to the same heartbeat and can wake up every so often and check when the last heartbeat happenned

Also, go step by step. DOn;t try to write out all the code at one. First implement your heartbeat, then implement heart that updates heartbeat without any randomization. Then implement Pacemaker and all it does is check the heartbeat without trying to apply any trigger, just log a message that it got a heartbeat. Then add randomization to the heart and see if pacemaker detects missed heartbeats, and then finish your pacemaker class


This is how I would do it. There are other ways to do it. You could have a queue between heart and pacemaker, for instance.


Thank you for your reply. I will try to implement your logic and get back here with result. Really appreciate your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi-Thread communication. if not data from thread -> time out.