Guest User

Untitled

a guest
Oct 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public class ContinuousTask extends AsyncTask<String,String,String> {
  2.  
  3.  
  4. private Stack<Runnable> runnables = new Stack<>();
  5. private Runnable onFinish;
  6.  
  7. @Override
  8. protected String doInBackground(String... params) {
  9.  
  10. while(!runnables.isEmpty())
  11. {
  12. runnables.pop().run();
  13. }
  14.  
  15. return null;
  16. }
  17.  
  18. @Override
  19. protected void onPostExecute(String s) {
  20. super.onPostExecute(s);
  21.  
  22. if(onFinish != null)
  23. onFinish.run();
  24. }
  25.  
  26. public ContinuousTask then(Runnable runnable)
  27. {
  28. runnables.push(runnable);
  29. return this;
  30. }
  31.  
  32. public ContinuousTask finish(Runnable runnable)
  33. {
  34. onFinish = runnable;
  35. return this;
  36. }
  37. }
  38.  
  39. new ContinuousTask().then(new Runnable() {
  40. @Override
  41. public void run() {
  42. // codigo 1
  43.  
  44. }
  45. }).then(new Runnable() {
  46. @Override
  47. public void run() {
  48. // codigo 2
  49.  
  50. }
  51. }).then(new Runnable() {
  52. @Override
  53. public void run() {
  54. // codigo 3
  55. }
  56. }).finish(new Runnable() {
  57. @Override
  58. public void run() {
  59. // metodo final
  60. }
  61. }).runTasks();// se ejecuta los tasks
Add Comment
Please, Sign In to add comment