All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.nps.moves.net.BehaviorConsumerThreaded Maven / Gradle / Ivy

Go to download

An open source implementation of the Distributed Interactive Simulation (DIS) IEEE-1278 protocol

There is a newer version: 5.8
Show newest version
package edu.nps.moves.net;

import java.util.*;

import edu.nps.moves.dis.*;

/**
 * Example threaded implementation of the BehaviorConsumerIF interface.

* * A threaded BehaviorConsumer should be used if it takes a long time to * process each DIS packet, or if you want to process the packets in your * own thread, rather than the thread used in the BehaviorProducer. * * @see BehaviorProducerIF * @see BehaviorConsumerIF * * @author DMcG */ public class BehaviorConsumerThreaded implements BehaviorConsumerIF, Runnable { private Vector receivedPdus; public BehaviorConsumerThreaded() { receivedPdus = new Vector(); } /** * Receives a PDU from the BehaviorProducer. * * @param pdu the ProtocolDataUnit generated by the BehaviorProducer */ public void receivePdu(Pdu pdu) { System.out.println("Got pdu from producer"); synchronized (receivedPdus) { receivedPdus.add(pdu); } } /** * Implementation of the Runnable interface */ public void run() { while (true) { while (true) // Yield the thread until we receive data { synchronized (receivedPdus) { if (receivedPdus.size() == 0) { Thread.yield(); } else { break; } } } // Print out how many we received, and create a new vector. synchronized (receivedPdus) { System.out.println("PDUs received: " + receivedPdus.size()); receivedPdus = new Vector(); } } // end of main wait loop } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy