2015年5月8日 星期五

[Android] avoid two different thread call a function in the same time

when two thread call the function of changeThumbnail in the part2, we can make a code like part1 into this function in order to avoid crash.

part1:
isReadyChgPhoto=true;
while(isReadyChgPhoto!=true){
 Log.e("please wait "+i +"sec","wait====================");
 i++;
 SystemClock.sleep(1000);
}
isReadyChgPhoto=false;

part2:
isReadyChgPhoto=true;

private void changeThumbnail(final int index, final int localport){
 new Thread(new Runnable() {
  public void run() {
   int i=0;
   while(isReadyChgPhoto!=true){
    Log.e("please wait "+i +"sec","wait====================");
    i++;
    SystemClock.sleep(1000);
   }
   isReadyChgPhoto=false;
   SystemClock.sleep(1000); 
   VLCApplication.setCurrUID(camIDArr[index]);   //for download thumbnail use
   
   String cmd_snapshot="http://127.0.0.1:"+localport+"/cgi-bin/cgi-cmd.cgi?command=snapshot";
   Log.e("%% get camera thumnail",cmd_snapshot+" uid: "+camIDArr[index]);

   new CGICommandForThumbnail(cmd_snapshot,"snapshot", localport, index).start(); 
   SystemClock.sleep(1000);
   isReadyChgPhoto=true;
   
  }
 }).start();
}

沒有留言:

張貼留言