iT邦幫忙

0

java.util.concurrent.CancellationException: Task was cancelled.

看到下午大神們的回覆, 經過一番思考, 我調整了一下程式, 嘗試將執行緒類別換成普通方法, 也嘗試將拆箱的array透過區域變數的方式進行調整, 也稍微檢查整一下後面接續的程式, 調整完後嘗試打印輸出, 雖然先前的問題已解決了,但程式運行後沒有看見輸出, 鑒於此, 我便開始設置中斷點進行偵錯, 但偵錯到一半, 程式突然終止 ,查看了錯誤訊息 ,發現下圖之情形 ,上網搜尋資料, 所找到的資訊甚少 ,所以上來發問

主程式碼:

byte[] temp = characteristic.getValue();

            byte[] increaseCapacity = new byte[temp.length];

            System.arraycopy(temp, 0, increaseCapacity, 0, temp.length);

            Byte[] byteObjects = new Byte[increaseCapacity.length];

            int i = 0;
            // Associating Byte array values with bytes. (byte[] to Byte[])
            for (byte b : increaseCapacity) {
                byteObjects[i++] = b;  // Autoboxing.....
            }
//            System.out.println("原始資料暫存器放入資料前 "+ rawDataList);
            rawDataList.addAll(Arrays.asList(byteObjects));
//            System.out.println("原始資料暫存器放入資料後 "+ rawDataList);
            byte[] bytes = new byte[rawDataList.size()];
            
            int j=0;

            for(Byte b: byteObjects)
                bytes[j++] = b.byteValue();

//            Process p1 = new Process();

//            StringBuilder stringBuilder = new StringBuilder(bytes.length);
//            for (byte byteChar : bytes)
//                stringBuilder.append(String.format("%02X ", byteChar));
//            System.out.println(stringBuilder.toString());

            size = bytes.length;

            codeStartTime = System.currentTimeMillis();
            for (int m = 0; m < size; m++) {
                if (m + 1 < size && m + 2 < size && m + 3 < size) {
                    if ((bytes[m] & 0xFF) == 2 && (bytes[m+1] & 0xFF) == 1 && (bytes[m+2] & 0xFF) == 4 && (bytes[m+3] & 0xFF) == 3) {
//                        System.out.println("跑檢查標頭判斷後原始資料暫存器資料 "+ rawDataList);
                        len = (bytes[m+8] & 0xFF);
                        len2 = (bytes[m+9] & 0xFF);
                        len3 = (bytes[m+10] & 0xFF);
                        len4 = (bytes[m+11] & 0xFF);

                        dataLen = len + len2 + len3 + len4 - 4;

                        if (m+dataLen+12 < size && m+dataLen+13 < size && m+dataLen+14 < size && m+dataLen+15 < size) {
                            if (bytes[m+dataLen+12] == -1 && bytes[m+dataLen+13] == -1 && bytes[m+dataLen+14] == -1 && bytes[m+dataLen+15] == -1)
                            {
//                                System.out.println("跑檢查標尾判斷後原始資料暫存器資料 "+ rawDataList);
                                threadStartTime = System.currentTimeMillis();
                                findHeadTail(bytes);
//                                p1.run();
                                threadEndTime = System.currentTimeMillis();
                                threadSeconds = (threadEndTime - threadStartTime) / 1000F;

                            }
                        }

                    }
                }
            }
            codeEndTime = System.currentTimeMillis();
            codeSeconds = (codeEndTime - codeStartTime) / 1000F;

//            if( codeSeconds > threadSeconds){
//                System.out.println("狀況正常 ");
//                System.out.println("執行緒共花 "+Float.toString(threadSeconds)+   " seconds. ");
//                System.out.println("整段程式共花 "+Float.toString(codeSeconds)+   " seconds. ");
//            }
//
//            else{
//                System.out.println("狀況不對 有問題 ");
//                System.out.println("執行緒共花 "+Float.toString(threadSeconds)+   " seconds. ");
//                System.out.println("整段程式共花 "+Float.toString(codeSeconds)+   " seconds. ");
//            }

方法:

public  void findHeadTail(byte[] array) {
            byte[] testBytes = new byte[array.length];
            int length = 0;
            int length2 = 0;
            int length3 = 0;
            int length4 = 0;
            int dataLength = 0;
            int size = array.length;

            for (int k = 0; k < size; k++) {
                if (k + 1 < size && k + 2 < size && k + 3 < size) {
                    if ((array[k] & 0xFF) == 2 && (array[k+1] & 0xFF) == 1 && (array[k+2] & 0xFF) == 4 && ( array[k+3] & 0xFF) == 3) {
//                        System.out.println("跑尋找標頭判斷後原始資料暫存器資料 "+ rawDataList);
                        length = ( array[k+8] & 0xFF);
                        length2 = (array[k+9] & 0xFF);
                        length3 = (array[k+10] & 0xFF);
                        length4 = (array[k+11] & 0xFF);

                        dataLength = length + length2 + length3 + length4 - 4;

                        if (k+dataLength+12 < size && k+dataLength+13 < size && k+dataLength+14 < size && k+dataLength+15 < size) {
                            if (array[k+dataLength+12] == -1 && array[k+dataLength+13] == -1 && array[k+dataLength+14] == -1 && array[k+dataLength+15] == -1)
                            {
//                                System.out.println("跑尋找標尾判斷後原始資料暫存器資料 "+ rawDataList);
//                                System.out.println("正確資料暫存器擷取資料前 "+ realDataList);
//                                tailCount++;
//                                System.out.println(tailCount);
                                for (int p=k+12; p<k+12+dataLength; p++){
                                    testBytes[p] = array[p];
//                                    realDataList.add(rawDataList.get(p));
                                }
//                                System.out.println("正確資料暫存器擷取資料後 "+ realDataList);

                                StringBuilder stringBuilder = new StringBuilder(testBytes.length);
                                for (byte byteChar : testBytes)
                                    stringBuilder.append(String.format("%02X ", byteChar));
                                System.out.println(stringBuilder.toString());
//                                printCount++;
//                                System.out.println(printCount);
//                                realDataList.clear();
                            }
                        }
                    }
                }
            }

        }

錯誤訊息:
https://ithelp.ithome.com.tw/upload/images/20211115/20143658i6Zd9Okwnd.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-11-16 09:35:24
最佳解答

將執行緒類別換成普通方法

如果單就 Exception 的名字java.util.concurrent.CancellationException來看
應該還有地方是用 thread 的方式寫

這篇有20種可能的寫法
如果程式裡有類似的寫法
可能就是出錯的地方

我要發表回答

立即登入回答