Guest User

Untitled

a guest
Oct 16th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. package ru.webanimal.academy_lesson04_threads;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5.  
  6. public class MainActivity extends AppCompatActivity {
  7.  
  8. //==============================================================================================
  9. // Static
  10. //==============================================================================================
  11.  
  12. private static final int FPS_LIMIT = 240;
  13.  
  14.  
  15. //==============================================================================================
  16. // Fields
  17. //==============================================================================================
  18.  
  19. private Thread tOne;
  20. private Thread tTwo;
  21. private final ThreadSwitch ts = new ThreadSwitch();
  22. private boolean isRunning = true;
  23.  
  24.  
  25. //==============================================================================================
  26. // Activity callbacks
  27. //==============================================================================================
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. createThreads();
  34. }
  35.  
  36. @Override
  37. protected void onStart() {
  38. super.onStart();
  39. startThreads();
  40. }
  41.  
  42. @Override
  43. protected void onStop() {
  44. stopThreads();
  45. super.onStop();
  46. }
  47.  
  48. //==============================================================================================
  49. // Private methods
  50. //==============================================================================================
  51.  
  52. private class LeftLeg implements Runnable {
  53. @Override
  54. public void run() {
  55. int fps = 0;
  56. while (isRunning) {
  57. if (fps > FPS_LIMIT) {
  58. synchronized (ts) {
  59. if (ts.get() % 2 == 0) {
  60. System.out.println("Left step");
  61. ts.raise(); // 0 ---> 1
  62. }
  63. fps = 0;
  64. }
  65. }
  66. fps++;
  67. }
  68. }
  69. }
  70.  
  71. private class RightLeg implements Runnable {
  72. @Override
  73. public void run() {
  74. int fps = 0;
  75. while (isRunning) {
  76. if (fps > FPS_LIMIT) {
  77. synchronized (ts) {
  78. if (ts.get() % 2 != 0) {
  79. System.out.println("Right step");
  80. ts.clear(); // 1 ---> 0
  81. }
  82. fps = 0;
  83. }
  84. }
  85. fps++;
  86. }
  87. }
  88. }
  89.  
  90. private void createThreads() {
  91. if (tOne == null) {
  92. tOne = new Thread(new LeftLeg());
  93. }
  94.  
  95. if (tTwo == null) {
  96. tTwo = new Thread(new RightLeg());
  97. }
  98. }
  99.  
  100. private void startThreads() {
  101. synchronized (ts) {
  102. ts.clear(); // Always LeftLeg is first on start
  103. }
  104. isRunning = true;
  105.  
  106. if (tOne != null && (!tOne.isAlive() || tOne.isInterrupted())) {
  107. tOne.start();
  108. }
  109.  
  110. if (tTwo != null && (!tTwo.isAlive() || tTwo.isInterrupted())) {
  111. tTwo.start();
  112. }
  113. }
  114.  
  115. private void stopThreads() {
  116. isRunning = false;
  117.  
  118. if (tOne != null && (tOne.isAlive() || !tOne.isInterrupted())) {
  119. tOne.interrupt();
  120. }
  121.  
  122. if (tTwo != null && (tTwo.isAlive() || !tTwo.isInterrupted())) {
  123. tTwo.interrupt();
  124. }
  125. }
  126. }
Add Comment
Please, Sign In to add comment