Remove all explicitly thread usage

This commit is contained in:
tarsin committed 2023-02-22 11:46:24 +08:00
1 parent ddccdc2ade
commit 37e3166f98
5 files changed
+6 -232

No files matched your search

@@ -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()
}
@@ -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<Runnable> 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<Runnable> {
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);
}
}
}
}
@@ -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();
}
}
}
@@ -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();
}
}
@@ -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);
}
}