Commit 187f70b7 by pye52

1、解决可能出现的死锁问题(通过扩大线程池)

2、增加日志输出覆盖位置(当前只用于排错)
parent 63fe3fc0
......@@ -2,13 +2,13 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.bgycc.smartcanteen"
minSdkVersion 22
targetSdkVersion 30
versionCode 143
versionName "1.4.3"
versionCode 144
versionName "1.4.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
......
......@@ -141,6 +141,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
});
payOnlineViewModel.getPayOnlineStateEvent().observe(this, event -> {
LogUtils.i(TAG, "在线支付状态: " + event);
switch (event.getState()) {
case PayOnlineState.IDLE:
message.setTextColor(getResources().getColor(R.color.qrcode_normal));
......@@ -167,6 +168,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
});
payOfflineViewModel.getPayOfflineStateEvent().observe(this, event -> {
LogUtils.i(TAG, "离线支付状态: " + event);
switch (event.getState()) {
case PayOfflineState.MARK:
message.setTextColor(getResources().getColor(R.color.pay_success));
......@@ -185,6 +187,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
});
qrCodeViewModel.getQRCodeStateEvent().observe(this, event -> {
LogUtils.i(TAG, "扫码状态: " + event);
switch (event.getState()) {
case QRCodeState.IDLE:
message.setTextColor(getResources().getColor(R.color.qrcode_normal));
......
......@@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
......@@ -18,20 +19,21 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class DefaultTaskExecutor extends TaskExecutor {
private static final String THREAD_NAME_STEM = "SmartCanteen_Thread_%d";
private final Object mLock = new Object();
private final AtomicInteger mThreadId = new AtomicInteger(0);
private final ScheduledExecutorService mThread = Executors.newScheduledThreadPool(6, new ThreadFactory() {
private static final String THREAD_NAME_STEM = "SmartCanteen_Thread_%d";
private final AtomicInteger mThreadId = new AtomicInteger(0);
private final ScheduledExecutorService mScheduledThread = Executors.newScheduledThreadPool(10, r -> {
Thread t = new Thread(r);
t.setName(String.format(Locale.CHINA, THREAD_NAME_STEM, mThreadId.getAndIncrement()));
return t;
});
@Override
public Thread newThread(@NotNull Runnable r) {
Thread t = new Thread(r);
t.setName(String.format(Locale.CHINA, THREAD_NAME_STEM, mThreadId.getAndIncrement()));
return t;
}
private final ExecutorService mCacheThread = Executors.newCachedThreadPool(r -> {
Thread t = new Thread(r);
t.setName(String.format(Locale.CHINA, THREAD_NAME_STEM, mThreadId.getAndIncrement()));
return t;
});
@Nullable
......@@ -39,27 +41,27 @@ public class DefaultTaskExecutor extends TaskExecutor {
@Override
public void executeOnDiskIO(@NotNull Runnable runnable) {
mThread.execute(runnable);
mCacheThread.execute(runnable);
}
@Override
public <T> Future<T> submit(Callable<T> callable) {
return mThread.submit(callable);
return mCacheThread.submit(callable);
}
@Override
public <T> Future<T> submit(Runnable runnable, T result) {
return mThread.submit(runnable, result);
return mCacheThread.submit(runnable, result);
}
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
return mThread.schedule(command, delay, unit);
return mScheduledThread.schedule(command, delay, unit);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
return mThread.scheduleAtFixedRate(command, initialDelay, period, unit);
return mScheduledThread.scheduleAtFixedRate(command, initialDelay, period, unit);
}
@Override
......@@ -81,6 +83,7 @@ public class DefaultTaskExecutor extends TaskExecutor {
@Override
public void quit() {
mThread.shutdownNow();
mCacheThread.shutdownNow();
mScheduledThread.shutdownNow();
}
}
\ No newline at end of file
......@@ -68,11 +68,11 @@ public class QRCodeViewModel extends ViewModel {
public void scan() {
if (scan == null) return;
if (scanFuture == null || scanFuture.isDone()) {
scanFuture = SCTaskExecutor.getInstance().schedule(scanRunnable, DELAY, TimeUnit.MILLISECONDS);
} else {
LogUtils.w(TAG, "当前已有扫码任务");
if (scanFuture != null) {
scanFuture.cancel(true);
scanFuture = null;
}
scanFuture = SCTaskExecutor.getInstance().schedule(scanRunnable, DELAY, TimeUnit.MILLISECONDS);
}
private Runnable scanRunnable = () -> {
......
......@@ -3,7 +3,7 @@ apply plugin: 'com.android.application'
def signed = "Daemon.apk"
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.bgycc.smartcanteen.daemon"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment