Skip to content

Commit

Permalink
把 Java 代码转换为 Kotlin 代码
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshaoaaa committed Jul 14, 2021
1 parent 225d169 commit 115f10c
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DispatchRunnable(private val mTask : Task,
Trace.beginSection(mTask.javaClass.simpleName)
DispatcherLog.i(
mTask.javaClass.simpleName
+ " begin run" + " Situation " + TaskStat.getCurrentSituation()
+ " begin run" + " Situation " + TaskStat.currentSituation
)
Process.setThreadPriority(mTask.priority())
var startTime = System.currentTimeMillis()
Expand Down Expand Up @@ -56,7 +56,7 @@ class DispatchRunnable(private val mTask : Task,
+ " needWait " + (mTask.needWait() || Looper.getMainLooper() == Looper.myLooper())
+ " ThreadId " + Thread.currentThread().id
+ " ThreadName " + Thread.currentThread().name
+ " Situation " + TaskStat.getCurrentSituation()
+ " Situation " + TaskStat.currentSituation
)
}
}
Expand Down
4 changes: 1 addition & 3 deletions appstarter/src/main/java/org/jay/appstarter/Task.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package org.jay.appstarter

import android.content.Context
import android.os.Process
import org.jay.appstarter.TaskDispatcher
import kotlin.jvm.Volatile
import org.jay.appstarter.utils.DispatcherExecutor
import org.jay.appstarter.TaskCallBack
import java.util.concurrent.CountDownLatch
import java.util.concurrent.ExecutorService

Expand Down Expand Up @@ -85,7 +83,7 @@ abstract class Task : ITask {
* CPU 密集型的一定要切换到 DispatcherExecutor.getCPUExecutor();
*/
override fun runOn(): ExecutorService {
return DispatcherExecutor.getIOExecutor()
return DispatcherExecutor.iOExecutor
}

/**
Expand Down
36 changes: 19 additions & 17 deletions appstarter/src/main/java/org/jay/appstarter/TaskDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ class TaskDispatcher private constructor() {
* 添加任务
*/
fun addTask(task: Task?): TaskDispatcher {
if (task != null) {
collectDepends(task)
mAllTasks.add(task)
mClsAllTasks.add(task.javaClass)
// 非主线程且需要wait的,主线程不需要CountDownLatch也是同步的
if (needWait(task)) {
mNeedWaitTasks.add(task)
mNeedWaitCount.getAndIncrement()
}
if (task == null) return this
collectDepends(task)
mAllTasks.add(task)
mClsAllTasks.add(task.javaClass)
// 非主线程且需要wait的,主线程不需要CountDownLatch也是同步的
if (needWait(task)) {
mNeedWaitTasks.add(task)
mNeedWaitCount.getAndIncrement()
}
return this
}
Expand Down Expand Up @@ -211,14 +210,17 @@ class TaskDispatcher private constructor() {
// 把任务添加到组线程任务列表中
mMainThreadTasks.add(task)
if (task.needCall()) {
task.setTaskCallBack {
TaskStat.markTaskDone()
task.isFinished = true
satisfyChildren(task)
markTaskDone(task)
DispatcherLog.i(task.javaClass.simpleName + " finish")
Log.i("testLog", "call")
}
task.setTaskCallBack(object : TaskCallBack {
override fun call() {
TaskStat.markTaskDone()
task.isFinished = true
satisfyChildren(task)
markTaskDone(task)
DispatcherLog.i(task.javaClass.simpleName + " finish")
Log.i("testLog", "call")
}

})
}
return
}
Expand Down
140 changes: 70 additions & 70 deletions appstarter/src/main/java/org/jay/appstarter/sort/TaskSortUtil.kt
Original file line number Diff line number Diff line change
@@ -1,113 +1,113 @@
package org.jay.appstarter.sort;
package org.jay.appstarter.sort

import androidx.annotation.NonNull;
import androidx.collection.ArraySet;
import org.jay.appstarter.Task;
import org.jay.appstarter.utils.DispatcherLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import androidx.collection.ArraySet
import org.jay.appstarter.Task
import org.jay.appstarter.utils.DispatcherLog
import java.util.ArrayList

// 任务排序工具类
public class TaskSortUtil {
object TaskSortUtil {

private static boolean sPrintAllTaskName = false;
private const val sPrintAllTaskName = false

// 高优先级的任务
private static List<Task> sNewTasksHigh = new ArrayList<>();
private val sNewTasksHigh: MutableList<Task> = ArrayList()

/**
* 任务的有向无环图的拓扑排序
*/
public static synchronized List<Task> getSortResult(List<Task> originTasks,
List<Class<? extends Task>> clsLaunchTasks) {
long makeTime = System.currentTimeMillis();

Set<Integer> dependSet = new ArraySet<>();
Graph graph = new Graph(originTasks.size());
for (int i = 0; i < originTasks.size(); i++) {
Task task = originTasks.get(i);
if (task.isSend() || task.dependsOn() == null || task.dependsOn().size() == 0) {
continue;
@Synchronized
fun getSortResult(
originTasks: List<Task>,
clsLaunchTasks: List<Class<out Task>>
): MutableList<Task> {
val makeTime = System.currentTimeMillis()
val dependSet: MutableSet<Int> = ArraySet()
val graph = Graph(originTasks.size)
for (i in originTasks.indices) {
val task = originTasks[i]
val list = task.dependsOn()
if (task.isSend || list == null || list.isEmpty()) {
continue
}
for (Class cls : task.dependsOn()) {
int indexOfDepend = getIndexOfTask(originTasks, clsLaunchTasks, cls);
if (indexOfDepend < 0) {
throw new IllegalStateException(task.getClass().getSimpleName() +
" depends on " + cls.getSimpleName() + " can not be found in task list ");
for (cls in task.dependsOn()!!) {
val indexOfDepend = getIndexOfTask(originTasks, clsLaunchTasks, cls)
check(indexOfDepend >= 0) {
task.javaClass.simpleName +
" depends on " + cls.simpleName + " can not be found in task list "
}
dependSet.add(indexOfDepend);
graph.addEdge(indexOfDepend, i);
dependSet.add(indexOfDepend)
graph.addEdge(indexOfDepend, i)
}
}
List<Integer> indexList = graph.topologicalSort();
List<Task> newTasksAll = getResultTasks(originTasks, dependSet, indexList);

DispatcherLog.i("task analyse cost makeTime " + (System.currentTimeMillis() - makeTime));
printAllTaskName(newTasksAll);
return newTasksAll;
val indexList: List<Int> = graph.topologicalSort()
val newTasksAll = getResultTasks(originTasks, dependSet, indexList)
DispatcherLog.i("task analyse cost makeTime " + (System.currentTimeMillis() - makeTime))
printAllTaskName(newTasksAll)
return newTasksAll
}

@NonNull
private static List<Task> getResultTasks(List<Task> originTasks,
Set<Integer> dependSet, List<Integer> indexList) {
List<Task> newTasksAll = new ArrayList<>(originTasks.size());
List<Task> newTasksDepended = new ArrayList<>();// 被别人依赖的
List<Task> newTasksWithOutDepend = new ArrayList<>();// 没有依赖的
List<Task> newTasksRunAsSoon = new ArrayList<>();// 需要提升自己优先级的,先执行(这个先是相对于没有依赖的先)
for (int index : indexList) {
private fun getResultTasks(
originTasks: List<Task>,
dependSet: Set<Int>, indexList: List<Int>
): MutableList<Task> {
val newTasksAll: MutableList<Task> = ArrayList(originTasks.size)
val newTasksDepended: MutableList<Task> = ArrayList() // 被别人依赖的
val newTasksWithOutDepend: MutableList<Task> = ArrayList() // 没有依赖的
val newTasksRunAsSoon: MutableList<Task> = ArrayList() // 需要提升自己优先级的,先执行(这个先是相对于没有依赖的先)
for (index in indexList) {
if (dependSet.contains(index)) {
newTasksDepended.add(originTasks.get(index));
newTasksDepended.add(originTasks[index])
} else {
Task task = originTasks.get(index);
val task = originTasks[index]
if (task.needRunAsSoon()) {
newTasksRunAsSoon.add(task);
newTasksRunAsSoon.add(task)
} else {
newTasksWithOutDepend.add(task);
newTasksWithOutDepend.add(task)
}
}
}

// 顺序:被别人依赖的————》需要提升自己优先级的————》需要被等待的————》没有依赖的
sNewTasksHigh.addAll(newTasksDepended);
sNewTasksHigh.addAll(newTasksRunAsSoon);
newTasksAll.addAll(sNewTasksHigh);
newTasksAll.addAll(newTasksWithOutDepend);
return newTasksAll;
sNewTasksHigh.addAll(newTasksDepended)
sNewTasksHigh.addAll(newTasksRunAsSoon)
newTasksAll.addAll(sNewTasksHigh)
newTasksAll.addAll(newTasksWithOutDepend)
return newTasksAll
}

private static void printAllTaskName(List<Task> newTasksAll) {
private fun printAllTaskName(newTasksAll: List<Task>) {
if (!sPrintAllTaskName) {
return;
return
}
for (Task task : newTasksAll) {
DispatcherLog.i(task.getClass().getSimpleName());
for (task in newTasksAll) {
DispatcherLog.i(task.javaClass.simpleName)
}
}

public static List<Task> getTasksHigh() {
return sNewTasksHigh;
}
val tasksHigh: List<Task>
get() = sNewTasksHigh

/**
* 获取任务在任务列表中的下标
*/
private static int getIndexOfTask(List<Task> originTasks,
List<Class<? extends Task>> clsLaunchTasks,
Class cls) {
int index = clsLaunchTasks.indexOf(cls);
private fun getIndexOfTask(
originTasks: List<Task>,
clsLaunchTasks: List<Class<out Task>>,
cls: Class<*>
): Int {
val index = clsLaunchTasks.indexOf(cls)
if (index >= 0) {
return index;
return index
}

// 仅仅是保护性代码
final int size = originTasks.size();
for (int i = 0; i < size; i++) {
if (cls.getSimpleName().equals(originTasks.get(i).getClass().getSimpleName())) {
return i;
val size = originTasks.size
for (i in 0 until size) {
if (cls.simpleName == originTasks[i].javaClass.simpleName) {
return i
}
}
return index;
return index
}
}
}
66 changes: 33 additions & 33 deletions appstarter/src/main/java/org/jay/appstarter/stat/TaskStat.kt
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package org.jay.appstarter.stat;
package org.jay.appstarter.stat

import org.jay.appstarter.utils.DispatcherLog.i
import kotlin.jvm.Volatile
import org.jay.appstarter.stat.TaskStatBean
import org.jay.appstarter.stat.TaskStat
import org.jay.appstarter.utils.DispatcherLog
import java.util.ArrayList
import java.util.concurrent.atomic.AtomicInteger

import org.jay.appstarter.utils.DispatcherLog;
object TaskStat {

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class TaskStat {

private static volatile String sCurrentSituation = "";
private static List<TaskStatBean> sBeans = new ArrayList<>();
private static AtomicInteger sTaskDoneCount = new AtomicInteger();
@Volatile
private var sCurrentSituation = ""
private val sBeans: MutableList<TaskStatBean> = ArrayList()
private var sTaskDoneCount = AtomicInteger()

// 是否开启统计
private static boolean sOpenLaunchStat = false;

public static String getCurrentSituation() {
return sCurrentSituation;
}

public static void setCurrentSituation(String currentSituation) {
if (!sOpenLaunchStat) {
return;
private const val sOpenLaunchStat = false

var currentSituation: String
get() = sCurrentSituation
set(currentSituation) {
if (!sOpenLaunchStat) {
return
}
i("currentSituation $currentSituation")
sCurrentSituation = currentSituation
setLaunchStat()
}
DispatcherLog.i("currentSituation " + currentSituation);
sCurrentSituation = currentSituation;
setLaunchStat();
}

public static void markTaskDone() {
sTaskDoneCount.getAndIncrement();
fun markTaskDone() {
sTaskDoneCount.getAndIncrement()
}

public static void setLaunchStat() {
TaskStatBean bean = new TaskStatBean();
bean.setSituation(sCurrentSituation);
bean.setCount(sTaskDoneCount.get());
sBeans.add(bean);
sTaskDoneCount = new AtomicInteger(0);
private fun setLaunchStat() {
val bean = TaskStatBean()
bean.situation = sCurrentSituation
bean.count = sTaskDoneCount.get()
sBeans.add(bean)
sTaskDoneCount = AtomicInteger(0)
}

}
}
29 changes: 5 additions & 24 deletions appstarter/src/main/java/org/jay/appstarter/stat/TaskStatBean.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
package org.jay.appstarter.stat;
package org.jay.appstarter.stat

class TaskStatBean {

private String situation;

private int count;

public String getSituation() {
return situation;
}

public void setSituation(String situation) {
this.situation = situation;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

}
internal class TaskStatBean {
var situation: String? = null
var count = 0
}
Loading

0 comments on commit 115f10c

Please sign in to comment.