Last active
January 25, 2019 19:53
-
-
Save nycodes9/53229d48328acf1ab9f7e1caa7cec70f to your computer and use it in GitHub Desktop.
Bolt task thread priority for Android demo
This file contains hidden or 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
package home.com.kitchensink; | |
import android.os.Bundle; | |
import android.os.Process; | |
import android.util.Log; | |
import java.util.Locale; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ThreadFactory; | |
import androidx.appcompat.app.AppCompatActivity; | |
import bolts.Task; | |
public class MainActivity extends AppCompatActivity { | |
ExecutorService mMoreFavExecutorService; | |
ExecutorService mLessFavExecutorService; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mMoreFavExecutorService = | |
Executors.newSingleThreadExecutor( | |
new ThreadFactory() { | |
@Override | |
public Thread newThread(Runnable r) { | |
return new Thread(r, "IOPool") { | |
@Override | |
public void run() { | |
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); | |
// Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO); | |
super.run(); | |
} | |
}; | |
} | |
}); | |
mLessFavExecutorService = | |
Executors.newSingleThreadExecutor( | |
new ThreadFactory() { | |
@Override | |
public Thread newThread(Runnable r) { | |
return new Thread(r, "IOPool") { | |
@Override | |
public void run() { | |
Thread.currentThread().setPriority(Thread.MIN_PRIORITY); | |
// Process.setThreadPriority(Process.THREAD_PRIORITY_VIDEO); | |
super.run(); | |
} | |
}; | |
} | |
}); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
for (int i = 0; i < 10; i++) { | |
final int finalI = i; | |
Task.call( | |
new Callable<Void>() { | |
@Override | |
public Void call() throws Exception { | |
Thread thread = Thread.currentThread(); | |
Thread.sleep(500); | |
String log = | |
String.format( | |
Locale.getDefault(), | |
"MainActivity#call() seq: %d JAVA thread: %s priority: %d ANDROID thread priority: %d", | |
finalI, | |
thread.getName(), | |
thread.getPriority(), | |
Process.getThreadPriority(Process.myTid())); | |
if (finalI % 2 == 0) { | |
Log.d("raj", log); | |
} else { | |
Log.i("raj", log); | |
} | |
return null; | |
} | |
}, | |
i % 2 == 0 ? mLessFavExecutorService : mMoreFavExecutorService); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment