Results 1 to 2 of 2
Thread: synchronizing troubles
- 10-12-2010, 09:04 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 22
- Rep Power
- 0
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)
{}
}
}
- 10-12-2010, 11:38 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Char troubles
By diggdude in forum New To JavaReplies: 2Last Post: 11-10-2009, 03:55 PM -
Integer troubles again...
By Tb0h in forum New To JavaReplies: 1Last Post: 09-20-2009, 02:32 AM -
JOptionPane Troubles
By Tb0h in forum New To JavaReplies: 4Last Post: 08-26-2009, 01:12 PM -
Integer troubles
By Tb0h in forum New To JavaReplies: 11Last Post: 08-13-2009, 04:56 PM -
how to synchronizing ArrayList
By sunilpatel28 in forum Advanced JavaReplies: 2Last Post: 12-10-2008, 11:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks