Skip to content

Commit

Permalink
优化不可用资源的匹配超时机制
Browse files Browse the repository at this point in the history
  • Loading branch information
ndroi committed Jul 24, 2020
1 parent 966b593 commit ab27bd3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Easy163/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "org.ndroi.easy163"
minSdkVersion 24
targetSdkVersion 28
versionCode 2
versionName "1.1"
versionCode 3
versionName "1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
19 changes: 12 additions & 7 deletions Easy163/app/src/main/java/org/ndroi/easy163/core/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.ndroi.easy163.providers.MiguMusic;
import org.ndroi.easy163.providers.Provider;
import org.ndroi.easy163.providers.QQMusic;
import org.ndroi.easy163.utils.ConcurrencyTask;
import org.ndroi.easy163.utils.Keyword;
import org.ndroi.easy163.utils.Song;

Expand All @@ -24,12 +25,12 @@ public class Search

public static Song search(Keyword keyword)
{
Log.d("search", "start to search: " + keyword.toString());
List<Song> songs = new ArrayList<>();
List<Thread> threads = new ArrayList<>();
ConcurrencyTask concurrencyTask = new ConcurrencyTask();
for (Provider provider : providers)
{
Thread thread = new Thread()
{
concurrencyTask.addTask(new Thread(){
@Override
public void run()
{
Expand All @@ -43,17 +44,21 @@ public void run()
}
}
}
};
thread.start();
threads.add(thread);
});
}
long startTime = System.currentTimeMillis();
/* just busy wait util first finish or 10 seconds */
/* just busy wait util first search successfully or all threads finish or 10 seconds */
while (songs.isEmpty())
{
if(concurrencyTask.isAllFinished())
{
Log.d("search", "all providers finish");
break;
}
long endTime = System.currentTimeMillis();
if (endTime - startTime > 10 * 1000)
{
Log.d("search", "timeout");
break;
}
try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.ndroi.easy163.hooks;

import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.ndroi.easy163.core.Cache;
import org.ndroi.easy163.hooks.utils.ConcurrencyTask;
import org.ndroi.easy163.utils.ConcurrencyTask;
import org.ndroi.easy163.utils.Crypto;
import org.ndroi.easy163.utils.Song;
import org.ndroi.easy163.vpn.hookhttp.Request;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
package org.ndroi.easy163.hooks.utils;

import java.util.ArrayList;
import java.util.List;

public class ConcurrencyTask
{
private List<Thread> threads = new ArrayList<>();

public void addTask(Thread thread)
{
threads.add(thread);
thread.start();
}

public void waitAll()
{
try
{
for (Thread thread : threads)
{
thread.join();
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
package org.ndroi.easy163.utils;

import java.util.ArrayList;
import java.util.List;

public class ConcurrencyTask
{
private List<Thread> threads = new ArrayList<>();

public void addTask(Thread thread)
{
threads.add(thread);
thread.start();
}

public boolean isAllFinished()
{
for (Thread thread : threads)
{
if(thread.isAlive())
{
return false;
}
}
return true;
}

public void waitAll()
{
try
{
for (Thread thread : threads)
{
thread.join();
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
11 changes: 11 additions & 0 deletions Easy163/app/src/main/java/org/ndroi/easy163/utils/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@ public class Keyword
{
public String songName;
public List<String> singers = new ArrayList<>();

@Override
public String toString()
{
String str = songName + ": ";
for (String singer : singers)
{
str += '/' + singer;
}
return str;
}
}

0 comments on commit ab27bd3

Please sign in to comment.