From 37e3166f98e6c6f18e044eb8c7a038e6e94dc87e Mon Sep 17 00:00:00 2001 From: tarsin Date: Wed, 22 Feb 2023 11:46:24 +0800 Subject: [PATCH] Remove all explicitly thread usage --- .../ehviewer/ui/fragment/AdvancedFragment.kt | 14 ++- .../com/hippo/util/IoThreadPoolExecutor.java | 92 ------------------- .../com/hippo/yorozuya/thread/PVLock.java | 50 ---------- .../hippo/yorozuya/thread/PriorityThread.java | 40 -------- .../thread/PriorityThreadFactory.java | 42 --------- 5 files changed, 6 insertions(+), 232 deletions(-) delete mode 100644 app/src/main/java/com/hippo/util/IoThreadPoolExecutor.java delete mode 100644 app/src/main/java/com/hippo/yorozuya/thread/PVLock.java delete mode 100644 app/src/main/java/com/hippo/yorozuya/thread/PriorityThread.java delete mode 100644 app/src/main/java/com/hippo/yorozuya/thread/PriorityThreadFactory.java diff --git a/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.kt b/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.kt index ccf2c085e..5605dcce5 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.kt @@ -16,7 +16,6 @@ package com.hippo.ehviewer.ui.fragment import android.annotation.SuppressLint -import android.app.Activity import android.content.Intent import android.net.Uri import android.os.Build @@ -41,10 +40,11 @@ import com.hippo.ehviewer.client.data.FavListUrlBuilder import com.hippo.ehviewer.client.parser.FavoritesParser import com.hippo.ehviewer.ui.scene.BaseScene import com.hippo.util.ExceptionUtils -import com.hippo.util.IoThreadPoolExecutor import com.hippo.util.LogCat import com.hippo.util.ReadableTime import com.hippo.yorozuya.IOUtils +import eu.kanade.tachiyomi.util.lang.launchIO +import eu.kanade.tachiyomi.util.lang.withUIContext import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking @@ -79,10 +79,9 @@ class AdvancedFragment : BasePreferenceFragment() { .setCancelable(false) .setView(R.layout.preference_dialog_task) .show() - IoThreadPoolExecutor.getInstance().execute { + lifecycleScope.launchIO { val success = EhDB.exportDB(requireActivity(), uri) - val activity: Activity? = activity - activity?.runOnUiThread { + withUIContext { if (alertDialog.isShowing) { alertDialog.dismiss() } @@ -204,10 +203,9 @@ class AdvancedFragment : BasePreferenceFragment() { .setCancelable(false) .setView(R.layout.preference_dialog_task) .show() - IoThreadPoolExecutor.getInstance().execute { + lifecycleScope.launchIO { val error = EhDB.importDB(requireActivity(), uri) - val activity: Activity? = activity - activity?.runOnUiThread { + withUIContext { if (alertDialog.isShowing) { alertDialog.dismiss() } diff --git a/app/src/main/java/com/hippo/util/IoThreadPoolExecutor.java b/app/src/main/java/com/hippo/util/IoThreadPoolExecutor.java deleted file mode 100644 index ace5b9fc0..000000000 --- a/app/src/main/java/com/hippo/util/IoThreadPoolExecutor.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2019 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.util; - -import androidx.annotation.NonNull; - -import com.hippo.yorozuya.thread.PriorityThreadFactory; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -public class IoThreadPoolExecutor extends ThreadPoolExecutor { - - private final static ThreadPoolExecutor INSTANCE = - IoThreadPoolExecutor.newInstance(3, 32, 1L, TimeUnit.SECONDS, - new PriorityThreadFactory("IO", android.os.Process.THREAD_PRIORITY_BACKGROUND)); - - private IoThreadPoolExecutor( - int corePoolSize, - int maximumPoolSize, - long keepAliveTime, - TimeUnit unit, - BlockingQueue workQueue, - ThreadFactory threadFactory, - RejectedExecutionHandler handler - ) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); - } - - public static ThreadPoolExecutor getInstance() { - return INSTANCE; - } - - private static ThreadPoolExecutor newInstance( - int corePoolSize, - int maximumPoolSize, - long keepAliveTime, - TimeUnit unit, - ThreadFactory threadFactory - ) { - ThreadQueue queue = new ThreadQueue(); - PutRunnableBackHandler handler = new PutRunnableBackHandler(); - IoThreadPoolExecutor executor = new IoThreadPoolExecutor( - corePoolSize, maximumPoolSize, keepAliveTime, unit, queue, threadFactory, handler); - queue.setThreadPoolExecutor(executor); - return executor; - } - - private static class ThreadQueue extends LinkedBlockingQueue { - - private ThreadPoolExecutor executor; - - void setThreadPoolExecutor(ThreadPoolExecutor executor) { - this.executor = executor; - } - - @Override - public boolean offer(@NonNull Runnable o) { - int allWorkingThreads = executor.getActiveCount() + super.size(); - return allWorkingThreads < executor.getPoolSize() && super.offer(o); - } - } - - public static class PutRunnableBackHandler implements RejectedExecutionHandler { - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { - try { - executor.getQueue().put(r); - } catch (InterruptedException e) { - throw new RejectedExecutionException(e); - } - } - } -} diff --git a/app/src/main/java/com/hippo/yorozuya/thread/PVLock.java b/app/src/main/java/com/hippo/yorozuya/thread/PVLock.java deleted file mode 100644 index 7c40843f2..000000000 --- a/app/src/main/java/com/hippo/yorozuya/thread/PVLock.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2015-2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.yorozuya.thread; - -public class PVLock { - - private int mCounter; - - public PVLock(int count) { - mCounter = count; - } - - /** - * Obtain - */ - public synchronized void p() throws InterruptedException { - while (true) { - if (mCounter > 0) { - mCounter--; - break; - } else { - this.wait(); - } - } - } - - /** - * Release - */ - public synchronized void v() { - mCounter++; - if (mCounter > 0) { - this.notify(); - } - } -} diff --git a/app/src/main/java/com/hippo/yorozuya/thread/PriorityThread.java b/app/src/main/java/com/hippo/yorozuya/thread/PriorityThread.java deleted file mode 100644 index e10452c6e..000000000 --- a/app/src/main/java/com/hippo/yorozuya/thread/PriorityThread.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2015-2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.yorozuya.thread; - -import android.os.Process; - -public class PriorityThread extends Thread { - - private final int mPriority; - - public PriorityThread(Runnable runnable, int priority) { - super(runnable); - mPriority = priority; - } - - public PriorityThread(Runnable runnable, String name, int priority) { - super(runnable, name); - mPriority = priority; - } - - @Override - public void run() { - Process.setThreadPriority(mPriority); - super.run(); - } -} diff --git a/app/src/main/java/com/hippo/yorozuya/thread/PriorityThreadFactory.java b/app/src/main/java/com/hippo/yorozuya/thread/PriorityThreadFactory.java deleted file mode 100644 index 197764e52..000000000 --- a/app/src/main/java/com/hippo/yorozuya/thread/PriorityThreadFactory.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2015-2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.yorozuya.thread; - -import androidx.annotation.NonNull; - -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * A thread factory that creates threads with a given thread priority. - */ -public class PriorityThreadFactory implements ThreadFactory { - - private final int mPriority; - private final AtomicInteger mIdGenerator = new AtomicInteger(); - private final String mName; - - public PriorityThreadFactory(String name, int priority) { - mName = name; - mPriority = priority; - } - - @Override - public Thread newThread(@NonNull Runnable r) { - return new PriorityThread(r, mName + "-" + mIdGenerator.getAndIncrement(), mPriority); - } -}