小编clo*_*ser的帖子

使用boolean var来停止线程

我有一本我正在学习的Java书,在其中一个例子中,我看到了一些可疑的东西.

public class ThreadExample extends MIDlet {
    boolean threadsRunning = true; // Flag stopping the threads

    ThreadTest thr1;
    ThreadTest thr2;

    private class ThreadTest extends Thread {
        int loops;

        public ThreadTest(int waitingTime) {
            loops = waitTime;
        }

        public void run() {
            for (int i = 1; i <= loops; i++) {
                if (threadsRunning != true) { // here threadsRunning is tested
                    return;
                }

                try {
                    Thread.sleep(1000);
                } catch(InterruptedException e) {
                    System.out.println(e);
                }
            }
        }
    }

    public ThreadExample() {
        thr1 = new …
Run Code Online (Sandbox Code Playgroud)

java multithreading midp cldc java-me

3
推荐指数
1
解决办法
2025
查看次数

标签 统计

cldc ×1

java ×1

java-me ×1

midp ×1

multithreading ×1