@@ -584,71 +584,94 @@ public ShimmerBluetooth(String userAssignedName, double samplingRate, int enable
584584 }
585585
586586 public class ProcessingThread extends Thread {
587- public boolean stop = false ;
587+ public volatile boolean stop = false ;
588588 int count =0 ;
589589 public void run () {
590- while (!stop ) {
591- if (!mABQPacketByeArray .isEmpty ()){
590+ while (!stop && !Thread .currentThread ().isInterrupted ()) {
591+ RawBytePacketWithPCTimeStamp rbp = null ;
592+ try {
593+ // Blocks (with a timeout) instead of busy-spinning on isEmpty() while idle.
594+ // The timeout ensures the stop flag is still checked periodically.
595+ rbp = mABQPacketByeArray .poll (100 , TimeUnit .MILLISECONDS );
596+ } catch (InterruptedException e ) {
597+ Thread .currentThread ().interrupt ();
598+ }
599+ if (rbp !=null ){
592600 count ++;
593601 if (count %1000 ==0 ){
594602 consolePrintLn ("Queue Size: " + mABQPacketByeArray .size ());
595603 printLogDataForDebugging ("Queue Size: " + mABQPacketByeArray .size () + "\n " );
596604 }
597- RawBytePacketWithPCTimeStamp rbp = mABQPacketByeArray .remove ();
598605// buildAndSendMsg(rbp.mDataArray, FW_TYPE_BT, false, rbp.mSystemTimeStamp);
599606 buildAndSendMsg (rbp .mDataArray , COMMUNICATION_TYPE .BLUETOOTH , false , rbp .mSystemTimeStamp );
600607 }
601- }
602- }
603- }
608+ }
609+ }
610+ }
604611
605612 //region --------- BLUETOOH STACK ---------
606613
607614 public class IOThread extends Thread {
608615 protected byte [] byteBuffer = {0 };
609- public boolean stop = false ;
616+ public volatile boolean stop = false ;
610617
611618 public void run () {
612- while (!stop ) {
619+ while (!stop && !Thread .currentThread ().isInterrupted ()) {
620+ boolean didWork = false ;
613621 //In-Shimmer Test here
614622 if (InShimmerTest ) {
615623 if (bytesAvailableToBeRead ()) {
616- byte [] data = readBytes (availableBytes ());
617- if (data !=null ) {
618- if (data .length >0 ) {
619- if (mTestByteListener != null ) {
620- mTestByteListener .eventNewBytesReceived (data );
621- }
624+ didWork = true ;
625+ byte [] data = readBytes (availableBytes ());
626+ if (data !=null ) {
627+ if (data .length >0 ) {
628+ if (mTestByteListener != null ) {
629+ mTestByteListener .eventNewBytesReceived (data );
630+ }
631+ }
622632 }
623633 }
624- }
625634 }
626635 else {
627636 if (mDummyRead ) {
628637 performDummyRead ();
638+ didWork = true ;
629639 }
630-
640+
631641 // Process Instruction on stack. is an instruction running? if not proceed
632642 if (!isInstructionStackLock ()){
633- processNextInstruction ();
643+ if (processNextInstruction ()){
644+ didWork = true ;
645+ }
634646 }
635-
647+
636648 if (mIsStreaming ){
637649 processWhileStreaming ();
650+ didWork = true ;
638651 }
639652 else if (bytesAvailableToBeRead ()){
653+ didWork = true ;
640654 if (mWaitForAck ) {
641655 processNotStreamingWaitForAck ();
642- }
656+ }
643657 else if (mWaitForResponse ) {
644658 processNotStreamingWaitForResp ();
645- }
646-
659+ }
660+
647661 processBytesAvailableAndInstreamSupported ();
648-
662+
663+ }
664+ }
665+
666+ if (!didWork ){
667+ // Nothing useful happened this iteration (idle, not streaming, no pending
668+ // instructions/bytes) - avoid busy-spinning and pegging a CPU core.
669+ try {
670+ Thread .sleep (2 );
671+ } catch (InterruptedException e ) {
672+ Thread .currentThread ().interrupt ();
649673 }
650674 }
651-
652675 }
653676 }
654677
@@ -681,10 +704,16 @@ private void performDummyRead() {
681704 mDummyReadStarted = false ;
682705 }
683706
684- private void processNextInstruction () {
707+ /**
708+ * @return true if an instruction was found and processed (i.e. useful work was done),
709+ * or if this call already idle-paced the loop with its own threadSleep(50) below -
710+ * in that case reporting true here stops run()'s outer idle-sleep from stacking an
711+ * additional wait on top of this one.
712+ */
713+ private boolean processNextInstruction () {
685714 // check instruction stack, are there any other instructions left to be executed?
686715 //checkAndRemoveFirstInstructionIfNull();
687-
716+
688717 byte [] insBytes = getInstruction ();
689718 if (insBytes !=null ){
690719 mCurrentCommand =insBytes [0 ];
@@ -761,14 +790,32 @@ else if((mCurrentCommand==GET_FW_VERSION_COMMAND)
761790
762791
763792 mTransactionCompleted =false ;
793+ return true ;
764794 } else {
795+ // getInstruction() returned null here - either the instruction stack is
796+ // genuinely empty, or its leading entry was a null placeholder that
797+ // getInstruction() just removed (a real instruction may still be next in
798+ // the list and will be picked up on the following iteration). Either way,
799+ // if there's also nothing else to do this iteration, wait here rather than
800+ // busy-spinning.
765801 if (!mIsStreaming && !bytesAvailableToBeRead ()){
766- threadSleep (50 );
802+ // Report "did work" (true) so the caller's own idle sleep in run()
803+ // doesn't stack an extra wait on top of this one.
804+ // Not threadSleep(): that helper swallows InterruptedException without
805+ // restoring the interrupt flag, which would defeat run()'s
806+ // !Thread.currentThread().isInterrupted() loop-exit check.
807+ try {
808+ Thread .sleep (50 );
809+ } catch (InterruptedException e ) {
810+ Thread .currentThread ().interrupt ();
811+ }
812+ return true ;
767813 }
814+ return false ;
768815 }
769816 }
770-
771- /** Process ACK from a GET or SET command while not streaming */
817+
818+ /** Process ACK from a GET or SET command while not streaming */
772819 private void processNotStreamingWaitForAck () {
773820 //JC TEST:: IMPORTANT TO REMOVE // This is to simulate packet loss
774821 /*
0 commit comments