android usb 讀寫以前都是一讀一寫,但有些機器會出問題。就採用讀寫非同步的方法。
使用物件鎖,object自帶的,然後使用object的方法wait和notify/notifyall
使用方法簡單,記錄下
public synchronized int lra_setregister(int index,int address, int value) ;
mcmdnow = 0x53;
cmd[5] = (byte)index;
cmd[6] = (byte)(address);
cmd[7] = (byte)(value);
byte cmd4send = crc16.data4send(cmd);
writeusbserial(cmd4send,cmd4send.length);
synchronized (syncobject) catch (interruptedexception e)
ret = mregistersetvalue;
}return ret;
}/**
* read register value by address
* @param address of registers need to read
* @return data in register
*/public synchronized int lra_getregister(int index,int address) ;
mcmdnow = 0x54;
cmd[5] = (byte)index;
cmd[6] = (byte)(address);
byte cmd4send = crc16.data4send(cmd);
writeusbserial(cmd4send,cmd4send.length);
synchronized (syncobject) catch (interruptedexception e)
ret = mregisterreadvalue;
}return ret;
}public byte lra_getdeviceid() ;
mcmdnow = (byte) 0xff;
writeusbserial(cmd,cmd.length);
synchronized (syncobject) catch (interruptedexception e)
}return mdeviceid;
}private boolean musbreadswitch = false;
private int musbreaddatalen = 20;
private byte musbreaddata = new byte[musbreaddatalen];
private byte musbreaddataclean = new byte[musbreaddatalen];
private byte mcmdnow = 0x00;
private object syncobject = new object();
//values return
private byte mdeviceid = new byte[13];
private byte mregisterreadvalue = 0x00;
private byte mregistersetvalue = 0x00;
class usbreadthread extends thread}}
}public byte ge***eviceid()
private int readreturndatafromusb(byte cmd, byte dataread)
break;
case 0x54:
mregisterreadvalue = dataread[5];
// mlistener.oneventcome(event.event_register_read_result_ok,mregisterreadvalue);
synchronized (syncobject)
break;
case (byte) 0xff:
system.arraycopy(dataread,4,mdeviceid,0,13);
showdatadetail(mdeviceid,"readreturndatafromusb");
// mlistener.oneventcome(event.event_deviceid_read_result,ret);
synchronized (syncobject)
break;
default:
break;
}}else
}else if((dataread[0] == 0x00) && (dataread[1] == 0x00) && (dataread[2] == 0x00) && (dataread[3] == 0x00) && (dataread[4] == 0x00)
&& (dataread[5] == 0x00))
}return ret;
}
Java學習 執行緒同步問題
多個執行緒買票 安全 public class unsafebuyticket implements runnable synchronized 鎖 執行緒安全實現機制 佇列 鎖 private synchronized void buyticket try catch interruptedexc...
java 執行緒 執行緒同步
threadlocal與其它同步機制的比較 threadlocal和其他所有的同步機制都是為了解決多執行緒中的對同一變數的訪問衝突。在普通的同步機制中,是通過對物件加鎖來實現多個執行緒對同一變數的安全訪問的。這時該變數是多個執行緒共享的,使用這種同步機制需要很細緻的分析在什麼時候對變數進行讀寫,什麼...
java同步執行緒
同步執行緒 synchronized方法 乙個類中任何方法都可以定義為synchronized方法以防止多執行緒資料崩潰。當某個物件用synchronized方法修飾時,表明該物件在任一時刻只能由乙個執行緒訪問。宣告方法體的一般格式 modifier synchronized returntype ...