對於我的應用我要跑兩個操作,無論是異步的,:如何同步2個異步線程/任務
從文件中讀取(我使用這個文件來模擬從數據總線讀) - 異步因爲我不知道「何時」在公共汽車上到達新的信息/字符。我搜索特定的序列字符,例如frame start_bytes =「xx」,後面的4個字節是我等待的「數據」。
將數據讀取/更新到Firebase,具體取決於從文件中讀取的「數據」 - 由於addValueEventListener的使用而導致的異步操作。
我想一個信號量/互斥機制或一個簡單的布爾標誌,一個任務信號,另一種是新的數據必須保存/更新到火力地堡。
如何將這兩個操作同步(通過將它們嵌入到Task/AsyncTask/Thread中)?
我跑了一個搜索這些主題,但我發現與UI,ProgressBars等相關的例子..不適合我的情況。在火力地堡
myRefDevices.addValueEventListener(new ValueEventListener() {
// addValueEventListener
// This method is called once with the initial value and again
// whenever data at this location is updated.
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean bChildFound = false;
DatabaseReference dbrefChildFound;
final CDeviceStatus obj_new = new CDeviceStatus();
for(DataSnapshot val : dataSnapshot.getChildren())
{
if(val.getKey().contentEquals(MAC_ADDRESS[ iIterator ]))
{
bChildFound = true;
dbrefChildFound = val.getRef();
obj_new.setiAvailable_A(val.getValue(CDeviceStatus.class).getiAvailable_A() + 1);
obj_new.setsID(val.getValue(CDeviceStatus.class).getsID());
dbrefChildFound.setValue(obj_new);
}
}
if(!bChildFound)
{
Log.d("child=" + MAC_ADDRESS[ iIterator ], "not found");
}
if(++iIterator == 16)
{
iIterator = 0;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
讀/更新數據從文件中讀取:
try {
// open input stream text file for reading
Resources res = getResources();
InputStream instream = res.openRawResource(R.raw.simulated_bus);
// we convert it to bufferred input stream
BufferedInputStream bistreamSimulatedBus = new BufferedInputStream(instream);
try{
// if we want to stop reading from the file/simulated bus for whatever reason..
boolean bStayInLoop = true;
while ((bistreamSimulatedBus.available() > 0) && bStayInLoop)
{
try {
// throw new InterruptedException();
char c = (char) bistreamSimulatedBus.read();
if(COUNT_CHARACTERS_NEWLINE)
{
if ('\n' == c){
// we can count how much NewLine character we have
//iNL_Counter++;
}
}
...
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
finally {
// release any resource associated with streams
if (null != instream) {
instream.close();
}
if (null != bistreamSimulatedBus) {
bistreamSimulatedBus.close();
}
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
謝謝。