Results 1 to 2 of 2
  1. #1
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default synchronizing troubles

    I am having concurrency issues, and am not sure if what to look for to fix it. I have a Gauge that will listen to a DataStreamManager (an object that holds data from a virtual device) peek at the data to see if it is for the gauge, pop it off store it and update a GUI accordingly. For the most part it works, except It waits until two datapackets are in the queue before popping them both off and updating.

    Also on the note of updating, as I understand it, I need a while loop in the run method to keep it running (duh) but I also have to place my update method in the loop. If the loop worked as It was suppose to this would be fine, but the gauge pops off the packet then loops without pause until two data packets are in the queue. At this point the gauge pops the packets update the data and waits for a moment and starts it pointless updating again.

    If you can see anything wrong please tell me, and thanks for helping.

    FROM THE GAUGE::
    protected boolean processPacket(DataPacket dP)
    {
    dSM.popPacket();
    Object [] data = dP.read();
    read = (Integer)data[3];
    return true;
    }

    public synchronized void run()
    {
    Log.i(CLASS_NAME, "Running as a thread.");

    while(isRUN)
    {
    while(!dSM.isEmpty())
    {
    if (dSM.peekPacket() == serial)
    processPacket(dSM.popPacket());
    }
    Log.i(CLASS_NAME, "Is Running.");
    update();
    postInvalidate();
    }
    }


    FROM THE DATA MANAGER
    public synchronized boolean pushPacket(DataPacket data)
    {
    if (packetQueue.size() >= maxPackets) return false; // Reject more than @maxPackets
    packetQueue.addLast(data);
    Log.w(CLASS_NAME, "A DataPacket has been added to the queue.");
    notifyAll();
    return true; // Packet accepted
    }
    public synchronized DataPacket popPacket()
    {
    Log.w(CLASS_NAME, "A DataPacket has been removed from the queue.");
    try{wait();} catch(Exception e){}
    return packetQueue.removeFirst();
    }

    /**
    *
    * @return :: the serial of the incoming device. DOES NOT REMOVE PACKET!
    */
    public synchronized String peekPacket()
    {
    try
    {
    DataPacket obj = packetQueue.peek();
    if (obj != null)
    return (String)obj.read()[1];
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    return null;
    }


    FROM THE VIRTUAL DEVICE
    public void run()
    {

    while (true)
    {
    update(LAST_SELECTION);
    dSM.pushPacket(new DataPacket(CLASS_NAME, "TEST_SERIAL", "PRESSURE", CURRENT_READING));
    try
    {
    t.sleep(2000);
    }
    catch(InterruptedException e)
    {}
    }
    }

  2. #2
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default Nevermind

    Nevermind on this. I figured it out, thanks anyway.

Similar Threads

  1. Char troubles
    By diggdude in forum New To Java
    Replies: 2
    Last Post: 11-10-2009, 04:55 PM
  2. Integer troubles again...
    By Tb0h in forum New To Java
    Replies: 1
    Last Post: 09-20-2009, 03:32 AM
  3. JOptionPane Troubles
    By Tb0h in forum New To Java
    Replies: 4
    Last Post: 08-26-2009, 02:12 PM
  4. Integer troubles
    By Tb0h in forum New To Java
    Replies: 11
    Last Post: 08-13-2009, 05:56 PM
  5. how to synchronizing ArrayList
    By sunilpatel28 in forum Advanced Java
    Replies: 2
    Last Post: 12-11-2008, 12:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •