thread task

v4
laoyuyu 2 years ago
parent e3b6b97c7c
commit 6f5e1db84c
  1. 12
      Http/src/main/java/com/arialyy/aria/http/download/HttpDTaskUtil.kt
  2. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/IBlockManager.java
  3. 14
      PublicComponent/src/main/java/com/arialyy/aria/core/task/DBlockManager.kt

@ -20,6 +20,8 @@ import com.arialyy.aria.core.DuaContext
import com.arialyy.aria.core.inf.IBlockManager import com.arialyy.aria.core.inf.IBlockManager
import com.arialyy.aria.core.task.AbsTaskUtil import com.arialyy.aria.core.task.AbsTaskUtil
import com.arialyy.aria.core.task.BlockManager import com.arialyy.aria.core.task.BlockManager
import com.arialyy.aria.core.task.TaskResp
import com.arialyy.aria.exception.AriaException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -47,7 +49,7 @@ internal class HttpDTaskUtil : AbsTaskUtil() {
} }
override fun stop() { override fun stop() {
TODO("Not yet implemented") blockManager?.stop()
} }
override fun start() { override fun start() {
@ -64,6 +66,14 @@ internal class HttpDTaskUtil : AbsTaskUtil() {
addCoreInterceptor(HttpDBlockInterceptor()) addCoreInterceptor(HttpDBlockInterceptor())
addCoreInterceptor(HttpBlockThreadInterceptor()) addCoreInterceptor(HttpBlockThreadInterceptor())
val resp = interceptor() val resp = interceptor()
if (resp == null || resp.code != TaskResp.CODE_SUCCESS) {
getTask().getTaskOption(HttpDTaskOption::class.java).taskListener.onFail(
false,
AriaException("start task fail, task interrupt, code: ${resp?.code ?: TaskResp.CODE_INTERRUPT}")
)
blockManager?.stop()
return@launch
}
Looper.loop() Looper.loop()
} }
} }

@ -42,6 +42,8 @@ public interface IBlockManager {
void start(List<IThreadTask> threadTaskList); void start(List<IThreadTask> threadTaskList);
void stop();
void setBlockNum(int blockNum); void setBlockNum(int blockNum);
void putUnfinishedBlock(BlockRecord record); void putUnfinishedBlock(BlockRecord record);

@ -30,6 +30,7 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadFactory
import java.util.concurrent.ThreadPoolExecutor import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit.MILLISECONDS import java.util.concurrent.TimeUnit.MILLISECONDS
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
@ -42,11 +43,12 @@ class BlockManager(task: ITask) : IBlockManager {
private val completedNum = AtomicInteger(0) // 完成的线程数 private val completedNum = AtomicInteger(0) // 完成的线程数
private val threadNum = task.getTaskOption(ITaskOption::class.java).threadNum private val threadNum = task.getTaskOption(ITaskOption::class.java).threadNum
private val scope = MainScope() private val scope = MainScope()
private val dispatcher = ThreadPoolExecutor( private val threadPool = ThreadPoolExecutor(
threadNum, threadNum, threadNum, threadNum,
0L, MILLISECONDS, 0L, MILLISECONDS,
LinkedBlockingQueue() LinkedBlockingQueue(),
).asCoroutineDispatcher() )
private val dispatcher = threadPool.asCoroutineDispatcher()
private var progress: Long = 0 //当前总进度 private var progress: Long = 0 //当前总进度
private lateinit var looper: Looper private lateinit var looper: Looper
@ -115,7 +117,7 @@ class BlockManager(task: ITask) : IBlockManager {
unfinishedBlock.add(record) unfinishedBlock.add(record)
} }
override fun getUnfinishedBlockList(): MutableList<BlockRecord> { override fun getUnfinishedBlockList(): List<BlockRecord> {
return unfinishedBlock return unfinishedBlock
} }
@ -135,6 +137,10 @@ class BlockManager(task: ITask) : IBlockManager {
} }
} }
override fun stop() {
quitLooper()
}
override fun setBlockNum(blockNum: Int) { override fun setBlockNum(blockNum: Int) {
this.blockNum = blockNum this.blockNum = blockNum
} }

Loading…
Cancel
Save