Я работаю над приложением для Android, которое имеет связь с устройством Bluetooth BLE (с пользовательским протоколом, который был разработан специально для нашего продукта).
после подключения к устройству у меня появляется обратный вызов public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
, который получаетсрабатывает каждый раз, когда устройство хочет отправить мне данные.В архитектуре приложения есть действия (например, getDeviceStatus()
), протокол работает следующим образом: телефон подключается к устройству -> сделать рукопожатие -> отправить команду (в виде байта) -> устройство отвечает командой (нормально / не хорошо) -> И теперь устройство начинает отправлять куски X byte[]
.Поэтому, чтобы убедиться, что мы обрабатываем данные в правильном порядке и не получаем путаницу из-за разных потоков, я добавил HandlerThread
:
HandlerThread handlerThread = new HandlerThread("ActionHandlerThread");
handlerThread.start();
Looper looper = handlerThread.getLooper();
mHandler = new Handler(looper);
и каждый раз, когда устройство отправляет данные, я звоню
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
mHandler.post(() -> {
mListener.onCharacteristicChanged(gatt, characteristic);
}
);
}
и mListener
отправляют данные другому классу, который проверяет, какие данные поступили и какие данные ожидаются.Если я правильно понимаю, что каждый раз, когда я вызываю post (), новая задача добавляется в петлитель обработчика и должна обрабатывать задачу в том же порядке, в котором я их добавил, но когда я запускаю приложение в журналах, я вижу, что обратный вызов срабатывает правильно и отправляет сообщение () вызывается в правильном порядке, но позже обработка происходит в неправильном порядке.Например, приложение ожидает получения команды подтверждения, но вместо этого данные уже пришли, но это невозможно, поскольку данные не могут быть получены до подтверждения.Основная причина, по которой я создаю HandlerThread, - убедиться, что вся операция с BLE будет выполняться в одном потоке, и не беспокоиться о проблемах синхронизации, но теперь, похоже, у меня проблемы с синхронизацией.Мне не хватает какой-то логики или понимания работы HandlerThread.
Некоторые тесты, которые я проводил с использованием только таймера и счетчика, мы видим, что, когда интервалы составляют всего 1 мс, не все задачи печатаются внутри сообщения:
Интервал 5 мс:
2019-01-30 21:33:31.531 27991-27991/? D/ThreadTest: Thread before Thread[main,5,main]
2019-01-30 21:33:31.532 27991-28036/? D/ThreadTest: Counter : 1 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.532 27991-28035/? D/ThreadTest: Counter : 1 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.537 27991-28036/? D/ThreadTest: Counter : 2 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.538 27991-28035/? D/ThreadTest: Counter : 2 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.543 27991-28036/? D/ThreadTest: Counter : 3 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.543 27991-28035/? D/ThreadTest: Counter : 3 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.548 27991-28036/? D/ThreadTest: Counter : 4 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.548 27991-28035/? D/ThreadTest: Counter : 4 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.553 27991-28036/? D/ThreadTest: Counter : 5 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.553 27991-28035/? D/ThreadTest: Counter : 5 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.559 27991-28036/? D/ThreadTest: Counter : 6 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.559 27991-28035/? D/ThreadTest: Counter : 6 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.563 27991-28036/? D/ThreadTest: Counter : 7 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.563 27991-28035/? D/ThreadTest: Counter : 7 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.568 27991-28036/? D/ThreadTest: Counter : 8 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.569 27991-28035/? D/ThreadTest: Counter : 8 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.574 27991-28036/? D/ThreadTest: Counter : 9 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.574 27991-28035/? D/ThreadTest: Counter : 9 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.580 27991-28036/? D/ThreadTest: Counter : 10 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.580 27991-28035/? D/ThreadTest: Counter : 10 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.585 27991-28036/? D/ThreadTest: Counter : 11 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.586 27991-28035/? D/ThreadTest: Counter : 11 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.591 27991-28036/? D/ThreadTest: Counter : 12 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.591 27991-28035/? D/ThreadTest: Counter : 12 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.596 27991-28036/? D/ThreadTest: Counter : 13 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.596 27991-28035/? D/ThreadTest: Counter : 13 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.602 27991-28036/? D/ThreadTest: Counter : 14 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.604 27991-28035/? D/ThreadTest: Counter : 14 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.607 27991-28036/? D/ThreadTest: Counter : 15 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.607 27991-28035/? D/ThreadTest: Counter : 15 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.612 27991-28036/? D/ThreadTest: Counter : 16 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.613 27991-28035/? D/ThreadTest: Counter : 16 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.618 27991-28036/? D/ThreadTest: Counter : 17 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.618 27991-28035/? D/ThreadTest: Counter : 17 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.623 27991-28036/? D/ThreadTest: Counter : 18 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.623 27991-28035/? D/ThreadTest: Counter : 18 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.628 27991-28036/? D/ThreadTest: Counter : 19 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.629 27991-28035/? D/ThreadTest: Counter : 19 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.633 27991-28036/? D/ThreadTest: Counter : 20 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.633 27991-28035/? D/ThreadTest: Counter : 20 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.638 27991-28036/? D/ThreadTest: Counter : 21 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.639 27991-28035/? D/ThreadTest: Counter : 21 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.643 27991-28036/? D/ThreadTest: Counter : 22 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.643 27991-28035/? D/ThreadTest: Counter : 22 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.648 27991-28036/? D/ThreadTest: Counter : 23 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.648 27991-28035/? D/ThreadTest: Counter : 23 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.654 27991-28036/? D/ThreadTest: Counter : 24 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.654 27991-28035/? D/ThreadTest: Counter : 24 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.659 27991-28036/? D/ThreadTest: Counter : 25 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.659 27991-28035/? D/ThreadTest: Counter : 25 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.665 27991-28036/? D/ThreadTest: Counter : 26 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.665 27991-28035/? D/ThreadTest: Counter : 26 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.669 27991-28036/? D/ThreadTest: Counter : 27 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.669 27991-28035/? D/ThreadTest: Counter : 27 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.674 27991-28036/? D/ThreadTest: Counter : 28 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.675 27991-28035/? D/ThreadTest: Counter : 28 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.679 27991-28036/? D/ThreadTest: Counter : 29 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.679 27991-28035/? D/ThreadTest: Counter : 29 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.684 27991-28036/? D/ThreadTest: Counter : 30 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.684 27991-28035/? D/ThreadTest: Counter : 30 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.690 27991-28036/? D/ThreadTest: Counter : 31 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.690 27991-28035/? D/ThreadTest: Counter : 31 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.694 27991-28036/? D/ThreadTest: Counter : 32 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.694 27991-28035/? D/ThreadTest: Counter : 32 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.700 27991-28036/? D/ThreadTest: Counter : 33 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.700 27991-28035/? D/ThreadTest: Counter : 33 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.704 27991-28036/? D/ThreadTest: Counter : 34 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.704 27991-28035/? D/ThreadTest: Counter : 34 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.709 27991-28036/? D/ThreadTest: Counter : 35 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.709 27991-28035/? D/ThreadTest: Counter : 35 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.715 27991-28036/? D/ThreadTest: Counter : 36 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.715 27991-28035/? D/ThreadTest: Counter : 36 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.719 27991-28036/? D/ThreadTest: Counter : 37 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.719 27991-28035/? D/ThreadTest: Counter : 37 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.724 27991-28036/? D/ThreadTest: Counter : 38 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.724 27991-28035/? D/ThreadTest: Counter : 38 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.730 27991-28036/? D/ThreadTest: Counter : 39 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.730 27991-28035/? D/ThreadTest: Counter : 39 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.735 27991-28036/? D/ThreadTest: Counter : 40 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.735 27991-28035/? D/ThreadTest: Counter : 40 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.740 27991-28036/? D/ThreadTest: Counter : 41 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.741 27991-28035/? D/ThreadTest: Counter : 41 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.745 27991-28036/? D/ThreadTest: Counter : 42 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.746 27991-28035/? D/ThreadTest: Counter : 42 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.750 27991-28036/? D/ThreadTest: Counter : 43 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.750 27991-28035/? D/ThreadTest: Counter : 43 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.756 27991-28036/? D/ThreadTest: Counter : 44 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.756 27991-28035/? D/ThreadTest: Counter : 44 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.760 27991-28036/? D/ThreadTest: Counter : 45 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.760 27991-28035/? D/ThreadTest: Counter : 45 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.765 27991-28036/? D/ThreadTest: Counter : 46 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.765 27991-28035/? D/ThreadTest: Counter : 46 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.771 27991-28036/? D/ThreadTest: Counter : 47 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.771 27991-28035/? D/ThreadTest: Counter : 47 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.775 27991-28036/? D/ThreadTest: Counter : 48 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.775 27991-28035/? D/ThreadTest: Counter : 48 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.780 27991-28036/? D/ThreadTest: Counter : 49 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.780 27991-28035/? D/ThreadTest: Counter : 49 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:33:31.785 27991-28036/? D/ThreadTest: Counter : 50 , Thread[Timer-0,5,main]
2019-01-30 21:33:31.786 27991-28035/? D/ThreadTest: Counter : 50 , inside post Thread[MyTestThread,5,main]
Интервал 1 мс:
2019-01-30 21:32:57.104 27831-27831/? D/ThreadTest: Thread before Thread[main,5,main]
2019-01-30 21:32:57.105 27831-27866/? D/ThreadTest: Counter : 1 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.106 27831-27865/? D/ThreadTest: Counter : 1 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.107 27831-27866/? D/ThreadTest: Counter : 2 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.107 27831-27865/? D/ThreadTest: Counter : 2 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.109 27831-27866/? D/ThreadTest: Counter : 3 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.110 27831-27865/? D/ThreadTest: Counter : 3 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.111 27831-27866/? D/ThreadTest: Counter : 4 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.111 27831-27865/? D/ThreadTest: Counter : 4 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.112 27831-27866/? D/ThreadTest: Counter : 5 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.112 27831-27865/? D/ThreadTest: Counter : 5 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.114 27831-27866/? D/ThreadTest: Counter : 6 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.114 27831-27866/? D/ThreadTest: Counter : 7 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.114 27831-27865/? D/ThreadTest: Counter : 7 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.115 27831-27865/? D/ThreadTest: Counter : 7 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.115 27831-27866/? D/ThreadTest: Counter : 8 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.116 27831-27865/? D/ThreadTest: Counter : 8 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.117 27831-27866/? D/ThreadTest: Counter : 9 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.117 27831-27866/? D/ThreadTest: Counter : 10 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.118 27831-27865/? D/ThreadTest: Counter : 10 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.118 27831-27865/? D/ThreadTest: Counter : 11 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.118 27831-27866/? D/ThreadTest: Counter : 11 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.118 27831-27865/? D/ThreadTest: Counter : 11 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.120 27831-27866/? D/ThreadTest: Counter : 12 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.120 27831-27865/? D/ThreadTest: Counter : 12 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.121 27831-27866/? D/ThreadTest: Counter : 13 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.122 27831-27865/? D/ThreadTest: Counter : 13 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.122 27831-27866/? D/ThreadTest: Counter : 14 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.123 27831-27865/? D/ThreadTest: Counter : 14 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.124 27831-27866/? D/ThreadTest: Counter : 15 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.125 27831-27865/? D/ThreadTest: Counter : 15 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.125 27831-27866/? D/ThreadTest: Counter : 16 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.126 27831-27865/? D/ThreadTest: Counter : 16 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.127 27831-27866/? D/ThreadTest: Counter : 17 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.127 27831-27866/? D/ThreadTest: Counter : 18 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.127 27831-27865/? D/ThreadTest: Counter : 18 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.128 27831-27865/? D/ThreadTest: Counter : 18 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.129 27831-27866/? D/ThreadTest: Counter : 19 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.129 27831-27866/? D/ThreadTest: Counter : 20 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.129 27831-27865/? D/ThreadTest: Counter : 20 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.129 27831-27865/? D/ThreadTest: Counter : 20 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.130 27831-27866/? D/ThreadTest: Counter : 21 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.130 27831-27865/? D/ThreadTest: Counter : 21 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.131 27831-27866/? D/ThreadTest: Counter : 22 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.131 27831-27865/? D/ThreadTest: Counter : 22 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.132 27831-27866/? D/ThreadTest: Counter : 23 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.133 27831-27866/? D/ThreadTest: Counter : 24 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.133 27831-27865/? D/ThreadTest: Counter : 24 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.133 27831-27865/? D/ThreadTest: Counter : 24 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.134 27831-27866/? D/ThreadTest: Counter : 25 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.134 27831-27865/? D/ThreadTest: Counter : 25 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.135 27831-27866/? D/ThreadTest: Counter : 26 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.135 27831-27865/? D/ThreadTest: Counter : 26 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.136 27831-27866/? D/ThreadTest: Counter : 27 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.136 27831-27865/? D/ThreadTest: Counter : 27 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.138 27831-27866/? D/ThreadTest: Counter : 28 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.138 27831-27865/? D/ThreadTest: Counter : 28 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.139 27831-27866/? D/ThreadTest: Counter : 29 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.139 27831-27865/? D/ThreadTest: Counter : 29 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.140 27831-27866/? D/ThreadTest: Counter : 30 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.140 27831-27865/? D/ThreadTest: Counter : 30 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.142 27831-27866/? D/ThreadTest: Counter : 31 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.142 27831-27865/? D/ThreadTest: Counter : 31 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.143 27831-27866/? D/ThreadTest: Counter : 32 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.143 27831-27865/? D/ThreadTest: Counter : 32 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.144 27831-27866/? D/ThreadTest: Counter : 33 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.145 27831-27865/? D/ThreadTest: Counter : 33 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.146 27831-27866/? D/ThreadTest: Counter : 34 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.146 27831-27865/? D/ThreadTest: Counter : 34 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.147 27831-27866/? D/ThreadTest: Counter : 35 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.147 27831-27865/? D/ThreadTest: Counter : 35 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.148 27831-27866/? D/ThreadTest: Counter : 36 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.148 27831-27865/? D/ThreadTest: Counter : 36 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.150 27831-27866/? D/ThreadTest: Counter : 37 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.150 27831-27866/? D/ThreadTest: Counter : 38 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.150 27831-27865/? D/ThreadTest: Counter : 38 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.150 27831-27865/? D/ThreadTest: Counter : 38 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.151 27831-27866/? D/ThreadTest: Counter : 39 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.151 27831-27865/? D/ThreadTest: Counter : 39 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.152 27831-27866/? D/ThreadTest: Counter : 40 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.152 27831-27865/? D/ThreadTest: Counter : 40 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.153 27831-27866/? D/ThreadTest: Counter : 41 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.154 27831-27866/? D/ThreadTest: Counter : 42 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.154 27831-27865/? D/ThreadTest: Counter : 42 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.154 27831-27865/? D/ThreadTest: Counter : 42 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.155 27831-27866/? D/ThreadTest: Counter : 43 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.155 27831-27865/? D/ThreadTest: Counter : 43 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.156 27831-27866/? D/ThreadTest: Counter : 44 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.156 27831-27865/? D/ThreadTest: Counter : 44 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.157 27831-27866/? D/ThreadTest: Counter : 45 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.158 27831-27865/? D/ThreadTest: Counter : 45 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.159 27831-27866/? D/ThreadTest: Counter : 46 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.159 27831-27865/? D/ThreadTest: Counter : 46 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.160 27831-27866/? D/ThreadTest: Counter : 47 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.160 27831-27865/? D/ThreadTest: Counter : 47 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.161 27831-27866/? D/ThreadTest: Counter : 48 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.161 27831-27865/? D/ThreadTest: Counter : 48 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.163 27831-27866/? D/ThreadTest: Counter : 49 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.163 27831-27866/? D/ThreadTest: Counter : 50 , Thread[Timer-0,5,main]
2019-01-30 21:32:57.163 27831-27865/? D/ThreadTest: Counter : 50 , inside post Thread[MyTestThread,5,main]
2019-01-30 21:32:57.163 27831-27865/? D/ThreadTest: Counter : 50 , inside post Thread[MyTestThread,5,main]
Код для теста:
private fun handlerThreadTest() {
val handlerThread = HandlerThread("MyTestThread")
handlerThread.start()
val handler = Handler(handlerThread.looper)
Log.d("ThreadTest", "Thread before ${Thread.currentThread()}")
var i = 0
val timerObj = Timer()
val timerTaskObj = object : TimerTask() {
override fun run() {
i++
//perform your action here
Log.d("ThreadTest", "Counter : $i , ${Thread.currentThread()}")
handler.post {
Log.d("ThreadTest", "Counter : $i , inside post ${Thread.currentThread()}")
if (i == 50)
timerObj.cancel()
}
}
}
timerObj.schedule(timerTaskObj, 0, 5)
}