Commit 187f70b7 by pye52

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

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