使用Android Studio时我遇到线程问题。 这是我的线程类:
class MainLoop extends Thread {
private boolean run = true;
@Override
public void run() {
System.out.println("Thread started");
run = true;
final double SPF = 1.0/2.0;
double deltaS;
long lastTime = System.nanoTime();
long newTime;
while(run) {
newTime = System.nanoTime();
deltaS = (float)(newTime-lastTime)/1000000000f;
if (deltaS > SPF) {
lastTime = newTime;
System.out.println("Hello from thread");
}
}
System.out.println("Thread killed");
}
void kill() {
System.out.println("Killing thread");
run = false;
}
}
这是我的MainActivity类:
public class MainActivity extends AppCompatActivity {
private MainLoop mainLoop = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLoop = new MainLoop();
mainLoop.start();
}
@Override
protected void onResume() {
super.onResume();
mainLoop = new MainLoop();
mainLoop.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mainLoop.kill();
return false;
}
}
这是我的输出:
08-15 12:24:07.480 2581-2667/com.lteii.hello I/System.out: Hello from thread
08-15 12:24:07.593 2581-2671/com.lteii.hello I/System.out: Hello from thread
08-15 12:24:07.595 2581-2581/com.lteii.hello D/ViewRootImpl@e1e91f8[MainActivity]: ViewPostImeInputStage processPointer 0
08-15 12:24:07.596 2581-2581/com.lteii.hello W/System: ClassLoader referenced unknown path: /system/framework/QPerformance.jar
08-15 12:24:07.597 2581-2581/com.lteii.hello E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
08-15 12:24:07.597 2581-2581/com.lteii.hello V/BoostFramework: BoostFramework() : mPerf = null
08-15 12:24:07.598 2581-2581/com.lteii.hello I/System.out: Killing thread
08-15 12:24:07.598 2581-2671/com.lteii.hello I/System.out: Thread killed
08-15 12:24:07.622 2581-2581/com.lteii.hello I/System.out: Killing thread
08-15 12:24:07.724 2581-2581/com.lteii.hello I/System.out: Killing thread
08-15 12:24:07.746 2581-2581/com.lteii.hello I/System.out: Killing thread
08-15 12:24:07.758 2581-2581/com.lteii.hello I/System.out: Killing thread
08-15 12:24:07.760 2581-2581/com.lteii.hello D/ViewRootImpl@e1e91f8[MainActivity]: ViewPostImeInputStage processPointer 1
08-15 12:24:07.760 2581-2581/com.lteii.hello I/System.out: Killing thread
08-15 12:24:07.980 2581-2667/com.lteii.hello I/System.out: Hello from thread
08-15 12:24:08.480 2581-2667/com.lteii.hello I/System.out: Hello from thread
我不明白它是怎么可能的,显然onResume()方法每次用onTouchEvent()方法杀死它都会重启线程,因为当我删除onResume()时,我再也看不到了“Hello from线程“之后”线程被杀“ 无论如何,我应该在“Thread killed”和“Hello from thread”之间看到“Thread created” 我需要帮助!
答案 0 :(得分:-1)
尝试以下方法:
public class MainActivity extends AppCompatActivity {
private MainLoop mainLoop = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mainLoop == null) {
mainLoop = new MainLoop();
mainLoop.start();
} else {
System.out.println("onCreate: Thread has not been killed!");
}
}
@Override
protected void onResume() {
super.onResume();
if (mainLoop == null) {
mainLoop = new MainLoop();
mainLoop.start();
} else {
System.out.println("onResume: Thread has not been killed!");
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mainLoop != null) {
mainLoop.kill();
mainLoop = null;
} else {
System.out.println("onTouchEvent: Thread has not been created!");
}
return false;
}
这应该解释这个问题。一般的好习惯是仔细检查自己的代码。在最好的情况下,永远不会调用那些双重检查,在最坏的情况下,你知道为什么出现问题。
作为旁注:您创建的线程在无限循环中运行,因此无需任何操作即可快速耗尽电池电量。至少使用Thread.sleep
或更好地使用ScheduledExecutor
或Timer
,如果您只有一个帖子。