-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Performance] Optimize get performance by stealing provider job while…
… waiting for result
- Loading branch information
Showing
6 changed files
with
184 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
provider/src/main/java/m/co/rh/id/aprovider/SyncWorkStealingWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package m.co.rh.id.aprovider; | ||
|
||
import android.util.Log; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* Special class to synchronize execution in a lock. | ||
* If lock is not yet acquired then it will try to steal and run task rather than waiting | ||
*/ | ||
class SyncWorkStealingWorker { | ||
|
||
private static final String TAG = SyncWorkStealingWorker.class.getName(); | ||
private ThreadPoolExecutor mThreadPoolExecutor; | ||
private ReentrantLock mLock; | ||
|
||
public SyncWorkStealingWorker(ThreadPoolExecutor threadPoolExecutor) { | ||
mThreadPoolExecutor = threadPoolExecutor; | ||
mLock = new ReentrantLock(true); | ||
} | ||
|
||
public <R> R submit(Callable<R> callable) { | ||
tryLock(); | ||
R result; | ||
try { | ||
result = callable.call(); | ||
} catch (Exception e) { | ||
Log.e(TAG, "Error executing task", e); | ||
throw new RuntimeException(e); | ||
} finally { | ||
mLock.unlock(); | ||
} | ||
return result; | ||
} | ||
|
||
public void execute(Runnable runnable) { | ||
tryLock(); | ||
try { | ||
runnable.run(); | ||
} catch (Exception e) { | ||
Log.e(TAG, "Error executing task", e); | ||
throw e; | ||
} finally { | ||
mLock.unlock(); | ||
} | ||
} | ||
|
||
private void tryLock() { | ||
while (!mLock.tryLock()) { | ||
try { | ||
Runnable poolRunnable = mThreadPoolExecutor.getQueue().poll(); | ||
if (poolRunnable != null) { | ||
poolRunnable.run(); | ||
} | ||
} catch (Exception e) { | ||
Log.e(TAG, "Error executing stolen task", e); | ||
throw e; | ||
} | ||
} | ||
} | ||
} |