Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. package com.example.test;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.view.View.OnClickListener;
  7.  
  8. public class MainActivity extends Activity implements OnClickListener {
  9.  
  10. @Override
  11. public void onCreate(Bundle saveInstanceState) {
  12. super.onCreate(saveInstanceState);
  13. Button button = new Button(this);
  14. button.setText("Do Time Consuming : ");
  15. setContentView(button);
  16. button.setOnClickListener(this);
  17. }
  18. @Override
  19. public void onClick(View v) {
  20. runTh();
  21. }
  22.  
  23. public void runTh() {
  24. new Thread(new Runnable() {
  25. @Override
  26. public void run() {
  27. try {
  28. for (int i = 0; i < 10; i++) {
  29. System.out.println(i);
  30. Thread.sleep(1000);
  31. }
  32. } catch (InterruptedException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }).start();
  37. }
  38. }
  39.  
  40. 2019-03-18 21:26:57.752 32757-32757/? I/zygote: Not late-enabling -Xcheck:jni (already on)
  41. 2019-03-18 21:26:58.269 32757-32757/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
  42. 2019-03-18 21:27:00.238 32757-32757/com.example.test I/InstantRun: starting instant run server: is main process
  43. 2019-03-18 21:27:00.954 32757-310/com.example.test D/OpenGLRenderer: HWUI GL Pipeline
  44. 2019-03-18 21:27:01.335 32757-310/com.example.test I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
  45. 2019-03-18 21:27:01.336 32757-310/com.example.test I/OpenGLRenderer: Initialized EGL, version 1.4
  46. 2019-03-18 21:27:01.337 32757-310/com.example.test D/OpenGLRenderer: Swap behavior 1
  47. 2019-03-18 21:27:01.340 32757-310/com.example.test W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
  48. 2019-03-18 21:27:01.340 32757-310/com.example.test D/OpenGLRenderer: Swap behavior 0
  49. 2019-03-18 21:27:01.366 32757-310/com.example.test D/EGL_emulation: eglCreateContext: 0xe0384e00: maj 3 min 0 rcv 3
  50. 2019-03-18 21:27:01.372 32757-310/com.example.test D/EGL_emulation: eglMakeCurrent: 0xe0384e00: ver 3 0 (tinfo 0xe03a8ca0)
  51. 2019-03-18 21:27:01.445 32757-310/com.example.test D/EGL_emulation: eglMakeCurrent: 0xe0384e00: ver 3 0 (tinfo 0xe03a8ca0)
  52. 2019-03-18 21:27:04.567 32757-316/com.example.test I/System.out: 0
  53. 2019-03-18 21:27:05.569 32757-316/com.example.test I/System.out: 1
  54. 2019-03-18 21:27:06.571 32757-316/com.example.test I/System.out: 2
  55. 2019-03-18 21:27:07.573 32757-316/com.example.test I/System.out: 3
  56. 2019-03-18 21:27:07.592 32757-317/com.example.test I/System.out: 0
  57. 2019-03-18 21:27:08.576 32757-316/com.example.test I/System.out: 4
  58. 2019-03-18 21:27:08.595 32757-317/com.example.test I/System.out: 1
  59. 2019-03-18 21:27:09.578 32757-316/com.example.test I/System.out: 5
  60. 2019-03-18 21:27:09.596 32757-317/com.example.test I/System.out: 2
  61. 2019-03-18 21:27:11.971 32757-316/com.example.test I/System.out: 6
  62. 2019-03-18 21:27:11.973 32757-317/com.example.test I/System.out: 3
  63. 2019-03-18 21:27:12.897 32757-318/com.example.test I/System.out: 0
  64. 2019-03-18 21:27:12.974 32757-316/com.example.test I/System.out: 7
  65. 2019-03-18 21:27:12.975 32757-317/com.example.test I/System.out: 4
  66. 2019-03-18 21:27:13.899 32757-318/com.example.test I/System.out: 1
  67. 2019-03-18 21:27:13.977 32757-316/com.example.test I/System.out: 8
  68. 2019-03-18 21:27:13.978 32757-317/com.example.test I/System.out: 5
  69. 2019-03-18 21:27:14.901 32757-318/com.example.test I/System.out: 2
  70. 2019-03-18 21:27:14.979 32757-316/com.example.test I/System.out: 9
  71. 2019-03-18 21:27:14.980 32757-317/com.example.test I/System.out: 6
  72. 2019-03-18 21:27:15.902 32757-318/com.example.test I/System.out: 3
  73. 2019-03-18 21:27:15.982 32757-317/com.example.test I/System.out: 7
  74. 2019-03-18 21:27:16.904 32757-318/com.example.test I/System.out: 4
  75. 2019-03-18 21:27:16.985 32757-317/com.example.test I/System.out: 8
  76. 2019-03-18 21:27:17.906 32757-318/com.example.test I/System.out: 5
  77. 2019-03-18 21:27:17.987 32757-317/com.example.test I/System.out: 9
  78. 2019-03-18 21:27:18.908 32757-318/com.example.test I/System.out: 6
  79. 2019-03-18 21:27:19.911 32757-318/com.example.test I/System.out: 7
  80. 2019-03-18 21:27:20.912 32757-318/com.example.test I/System.out: 8
  81. 2019-03-18 21:27:21.915 32757-318/com.example.test I/System.out: 9
  82.  
  83. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  84.  
  85. private CounterThread mCounterThread;
  86.  
  87. @Override
  88. public void onCreate(Bundle savedInstanceState) {
  89. super.onCreate(savedInstanceState);
  90. Button button = new Button(this);
  91. button.setText("Do Time Consuming : ");
  92. setContentView(button);
  93. button.setOnClickListener(this);
  94. }
  95.  
  96. @Override
  97. public void onClick(View v) {
  98. runTh();
  99. }
  100.  
  101. public void runTh() {
  102. if (mCounterThread == null || mCounterThread.getState() == Thread.State.TERMINATED) {
  103. // If thread is null or terminated we will create it.
  104. mCounterThread = new CounterThread();
  105. }
  106.  
  107. if (!mCounterThread.isAlive()) {
  108. // Start the thread if it is in NEW state.
  109. mCounterThread.start();
  110. } else {
  111. // Reset counter if it is in RUNNING state.
  112. mCounterThread.resetCounter();
  113. }
  114. }
  115.  
  116. class CounterThread extends Thread {
  117.  
  118. @Override
  119. public void run() {
  120. try {
  121. for (int i = 0; i < 10; i++) {
  122. System.out.println(i);
  123. Thread.sleep(1000);
  124. }
  125. } catch (InterruptedException e) {
  126. // Caught InterruptedException then ignore current counter and start new counter from 0.
  127. run();
  128. }
  129. }
  130.  
  131. void resetCounter() {
  132. // Throw a InterruptedException to this thread.
  133. this.interrupt();
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement