java - 线程似乎神奇地开始了

标签 java android multithreading

我在使用 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

我不明白这是怎么可能的,显然,每次我用 onTouchEvent() 方法杀死线程时,onResume() 方法都会重新启动线程,因为当我删除 onResume() 时,我不再看到“Hello from “线程被杀死”之后的“线程” 无论如何,我应该在“线程被杀死”和“线程问候”之间看到“线程创建” 我需要帮助!

最佳答案

尝试以下操作:

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(如果您只有一个线程)。

关于java - 线程似乎神奇地开始了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45691085/

相关文章:

Python SARIMA 模型自动使用 CPU 的所有核心。如何?

java - 具有固定数量子项的 CardView

java - 如何设置多语言文本到语音

java - 如何将平面 JSON 转换为分层 java 类?

java - Android 中的文本字段

java - EJB 中基于 Zookeeper 的锁

java - 如何在Android中单独显示openweather map 数据?

java - Android获取ArrayList中的所有联系人电话号码

android - 避免多个绘图集 (hdpi/mdpi/ldpi)

multithreading - 如何设计真实世界的模拟?