我写了这个应用程序,它的计数从0到9。在logcat中,当我运行该应用程序并单击它在屏幕上时,它的计数是正常的,但是当我双击屏幕时,它变成了嵌套计数。如何解决该问题,以便每当我单击按钮时,它便从0开始计数并停止先前的计数线程
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
Button button = new Button(this);
button.setText("Do Time Consuming : ");
setContentView(button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
runTh();
}
public void runTh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println(i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
这是logcat:
2019-03-18 21:26:57.752 32757-32757/? I/zygote: Not late-enabling -Xcheck:jni (already on)
2019-03-18 21:26:58.269 32757-32757/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
2019-03-18 21:27:00.238 32757-32757/com.example.test I/InstantRun: starting instant run server: is main process
2019-03-18 21:27:00.954 32757-310/com.example.test D/OpenGLRenderer: HWUI GL Pipeline
2019-03-18 21:27:01.335 32757-310/com.example.test I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-03-18 21:27:01.336 32757-310/com.example.test I/OpenGLRenderer: Initialized EGL, version 1.4
2019-03-18 21:27:01.337 32757-310/com.example.test D/OpenGLRenderer: Swap behavior 1
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...
2019-03-18 21:27:01.340 32757-310/com.example.test D/OpenGLRenderer: Swap behavior 0
2019-03-18 21:27:01.366 32757-310/com.example.test D/EGL_emulation: eglCreateContext: 0xe0384e00: maj 3 min 0 rcv 3
2019-03-18 21:27:01.372 32757-310/com.example.test D/EGL_emulation: eglMakeCurrent: 0xe0384e00: ver 3 0 (tinfo 0xe03a8ca0)
2019-03-18 21:27:01.445 32757-310/com.example.test D/EGL_emulation: eglMakeCurrent: 0xe0384e00: ver 3 0 (tinfo 0xe03a8ca0)
2019-03-18 21:27:04.567 32757-316/com.example.test I/System.out: 0
2019-03-18 21:27:05.569 32757-316/com.example.test I/System.out: 1
2019-03-18 21:27:06.571 32757-316/com.example.test I/System.out: 2
2019-03-18 21:27:07.573 32757-316/com.example.test I/System.out: 3
2019-03-18 21:27:07.592 32757-317/com.example.test I/System.out: 0
2019-03-18 21:27:08.576 32757-316/com.example.test I/System.out: 4
2019-03-18 21:27:08.595 32757-317/com.example.test I/System.out: 1
2019-03-18 21:27:09.578 32757-316/com.example.test I/System.out: 5
2019-03-18 21:27:09.596 32757-317/com.example.test I/System.out: 2
2019-03-18 21:27:11.971 32757-316/com.example.test I/System.out: 6
2019-03-18 21:27:11.973 32757-317/com.example.test I/System.out: 3
2019-03-18 21:27:12.897 32757-318/com.example.test I/System.out: 0
2019-03-18 21:27:12.974 32757-316/com.example.test I/System.out: 7
2019-03-18 21:27:12.975 32757-317/com.example.test I/System.out: 4
2019-03-18 21:27:13.899 32757-318/com.example.test I/System.out: 1
2019-03-18 21:27:13.977 32757-316/com.example.test I/System.out: 8
2019-03-18 21:27:13.978 32757-317/com.example.test I/System.out: 5
2019-03-18 21:27:14.901 32757-318/com.example.test I/System.out: 2
2019-03-18 21:27:14.979 32757-316/com.example.test I/System.out: 9
2019-03-18 21:27:14.980 32757-317/com.example.test I/System.out: 6
2019-03-18 21:27:15.902 32757-318/com.example.test I/System.out: 3
2019-03-18 21:27:15.982 32757-317/com.example.test I/System.out: 7
2019-03-18 21:27:16.904 32757-318/com.example.test I/System.out: 4
2019-03-18 21:27:16.985 32757-317/com.example.test I/System.out: 8
2019-03-18 21:27:17.906 32757-318/com.example.test I/System.out: 5
2019-03-18 21:27:17.987 32757-317/com.example.test I/System.out: 9
2019-03-18 21:27:18.908 32757-318/com.example.test I/System.out: 6
2019-03-18 21:27:19.911 32757-318/com.example.test I/System.out: 7
2019-03-18 21:27:20.912 32757-318/com.example.test I/System.out: 8
2019-03-18 21:27:21.915 32757-318/com.example.test I/System.out: 9
答案 0 :(得分:0)
如果您更喜欢使用Thread类而不是RxJava,则可以保存线程实例并在启动新实例之前将其停止
答案 1 :(得分:0)
问题:每次单击按钮时,都会创建一个新线程,并从0开始计数。
解决方案::您可以定义一个从Thread
扩展的类,然后单击按钮时检查,如果线程正在运行,则将其重置。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CounterThread mCounterThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Do Time Consuming : ");
setContentView(button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
runTh();
}
public void runTh() {
if (mCounterThread == null || mCounterThread.getState() == Thread.State.TERMINATED) {
// If thread is null or terminated we will create it.
mCounterThread = new CounterThread();
}
if (!mCounterThread.isAlive()) {
// Start the thread if it is in NEW state.
mCounterThread.start();
} else {
// Reset counter if it is in RUNNING state.
mCounterThread.resetCounter();
}
}
class CounterThread extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println(i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// Caught InterruptedException then ignore current counter and start new counter from 0.
run();
}
}
void resetCounter() {
// Throw a InterruptedException to this thread.
this.interrupt();
}
}
}