0

Scenario:

I want to test a communication between 2 devices. They communicate by frames.

I start up the application (on device 1) and I send a number of frames (each frames contains a unique (int) ID). Device 2 receives each frame and sends an acknowledgement (and just echo's the ID) or it doesn't. (when frame got lost)

When device 1 receives the ACK I want to compare the time it took to send and receive the ACK back.

From looking around SO How do I measure time elapsed in Java? System.nanoTime() is probably the best way to monitor the elapsed time. However this is all happening in different threads according to the classic producer-consumer pattern where a thread (on device 1) is always reading and another is managing the process (and also writing the frames). Now thank you for bearing with me my question is:

Question: Now for the problem: I need to convey the unique ID from the ACK frame from the reading thread to the managing thread. I've done some research and this seems to be an good candidate for wait/notify system or not? Or perhaps I just need a shared array that contains data of each frame send? But than how does the managing thread know it happened?

Context I want to compare these times because I want to research what factors can hamper communication.

Community
  • 1
  • 1
Thomas
  • 1,678
  • 4
  • 24
  • 47

2 Answers2

1

Why don't you just populate a shared map with <unique id, timestamp> pairs? You can expire old entries by periodically removing entries older than a certain amount.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • And then have a timer in the managing thread to react on ACK? Because I would like to work in phases of sending more and more data and seing how the devices coops with that stress? – Thomas Aug 01 '13 at 13:26
  • In this case hi's better off with Guava's LoadingCache. – allprog Aug 01 '13 at 13:26
  • would you advocate a ConcurrentMap or a simple map? – Thomas Aug 02 '13 at 06:59
0

I suggest you reformulate your problem with tasks (Callable). Create a task for the writer and one for the reader role. Submit these in pairs in an ExecutorService and let the Java concurrency framework handle the concurrency for you. You only have to think about what will be the result of a task and how would you want to use it.

// Pseudo code
ExecutorService EXC = Executors.newCachedThreadPool();
Future<List<Timestamp>> readerFuture = EXC.submit(new ReaderRole(sentFramwNum));
Future<List<Timestamp>> writerFuture = EXC.submit(new WriterRole(sentFrameNum)); 

List<Timestamp>  writeResult = writerFuture.get(); // wait for the completion of writing
List<Timestamp> readResult = readerFuture.get(); // wait for the completion of reading

This is pretty complex stuff but much cleaner and more stable that a custom developed synchronization solution.

Here is a pretty good tutorial for the Java concurrency framework: http://www.vogella.com/articles/JavaConcurrency/article.html#threadpools

allprog
  • 16,540
  • 9
  • 56
  • 97