From 1aa93c4d99d7623327d98a8d66cf6c4d2bcba50c Mon Sep 17 00:00:00 2001
From: terry-norbraten <tnorb@comcast.net>
Date: Fri, 26 Jun 2020 16:59:51 -0600
Subject: [PATCH] rework for threading

---
 .../nps/moves/dis7/examples/EntityUse.java    | 30 +++++++++++--------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/src/edu/nps/moves/dis7/examples/EntityUse.java b/src/edu/nps/moves/dis7/examples/EntityUse.java
index 855012079f..298c8e9fbf 100644
--- a/src/edu/nps/moves/dis7/examples/EntityUse.java
+++ b/src/edu/nps/moves/dis7/examples/EntityUse.java
@@ -75,6 +75,11 @@ public class EntityUse
    */
   public static void exampleUse() throws Exception
   {
+    DisThreadedNetIF netif = new DisThreadedNetIF(); // uses defaults
+    
+    // We want to listen also, so add a listener, using JDK8+ lambda grammar
+    netif.addListener(pdu->handleReceivedPdu(pdu));
+    
     PduFactory pduFactory = new PduFactory();  // uses defaults: usa, exercise id 1, site id 2, app id 3, absolute time
 
     EntityStatePdu espdu = pduFactory.makeEntityStatePdu();  
@@ -88,27 +93,28 @@ public class EntityUse
     
     espdu.setEntityType(et);
     
-    DisThreadedNetIF netif = new DisThreadedNetIF(); // uses defaults
-    
-    // We want to listen also, so add a listener, using JDK8+ lambda grammar
-    netif.addListener(pdu->handleReceivedPdu(pdu));
     netif.send(espdu);  // possibly throws IOException
+    sleep(100L);
     
     /* Do the same for the second way of creating a Shenandoah entity type and show an alternate way of creating an ESPDU */
     
-    EntityStatePdu espdu2 = (EntityStatePdu)pduFactory.createPdu(DISPDUType.ENTITY_STATE);
+    espdu = (EntityStatePdu)pduFactory.createPdu(DISPDUType.ENTITY_STATE);
     /* set desired entity state fields here */
 
     edu.nps.moves.dis7.entities.usa.platform.surface.AD44Shenandoah et2 = new edu.nps.moves.dis7.entities.usa.platform.surface.AD44Shenandoah();
     /* Use import statement to make the code above more readable */
     
-    espdu2.setEntityType(et2);
-    netif.send(espdu2);  // possibly throws IOException
-    
-    // Wait a bit to see output
-    try { 
-        Thread.sleep(250L);
-    } catch(InterruptedException ex) {}
+    espdu.setEntityType(et2);
+    netif.send(espdu);  // possibly throws IOException
+    sleep(100L);
+  }
+  
+  private static void sleep(long ms)
+  {
+    try {
+        Thread.sleep(ms);
+    }
+    catch (InterruptedException ex) {}
   }
   
   private static void handleReceivedPdu(Pdu pdu)
-- 
GitLab