Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 控制线程通过按钮_Java_Blackberry - Fatal编程技术网

Java 控制线程通过按钮

Java 控制线程通过按钮,java,blackberry,Java,Blackberry,我们需要一段代码来控制线程。例如,使用三个按钮,如开始、停止和暂停,按下其中一个按钮并对其执行操作。与按开始然后启动线程一样,按停止实际上会停止线程,按暂停分别执行暂停操作。使用thread.start()启动线程很简单。停止线程可以像设置在run方法中异步检查的标志一样简单,但可能需要包括调用thread.interrupt()。暂停线程的问题更大,但也可以使用一个标志来完成,该标志会使run方法产生而不是进程。以下是一些(未经测试的)代码: 正如您所见,根据您在线程中所做的工作,它可能很快变

我们需要一段代码来控制线程。例如,使用三个按钮,如开始、停止和暂停,按下其中一个按钮并对其执行操作。与按开始然后启动线程一样,按停止实际上会停止线程,按暂停分别执行暂停操作。

使用
thread.start()
启动线程很简单。停止线程可以像设置在run方法中异步检查的标志一样简单,但可能需要包括调用
thread.interrupt()
。暂停线程的问题更大,但也可以使用一个标志来完成,该标志会使run方法产生而不是进程。以下是一些(未经测试的)代码:


正如您所见,根据您在线程中所做的工作,它可能很快变得复杂。

我想补充Richard的答案,以解决一些问题:

  • 暂停时不必要的循环
  • 状态改变时不需要额外的循环
  • yield()
    在需要时使用
  • 单实例
  • 停止线程将等待线程完成
  • 这是我修改过的代码:

    class MyThread extends Thread {
        private final static int STATE_RUN = 0, STATE_PAUSE = 2, STATE_STOP = 3;
        private int _state;
    
        private static MyThread thread;
    
        public static MyThread getInstance() {
            if (thread == null || !thread.isAlive()) {
                thread = new MyThread();
            }
            return thread;
        }
    
    
        private MyThread() {
            _state = STATE_RUN;
        }
    
        public static void main(String[] args) {
            MyThread t = MyThread.getInstance();
            try {
                t.start();
                Thread.sleep(500);
                t.pause();
                Thread.sleep(500);
                t.unpause();
                Thread.sleep(500);
                t.end();
            } catch (InterruptedException e) {
                // ignore; this is just an example
            }
        }
    
        public void run() {
            int i = 0;
            while (_state != STATE_STOP) {
                if (_state == STATE_PAUSE) {
                    System.out.println(this + " paused");
                    synchronized (this) {
                        try {
                            this.wait();
                        } catch (InterruptedException e) {
                        }
                    }
                }
                if (_state == STATE_STOP) {
                    break;
                }
    
                // this is where the actual processing happens
                try {
                    // slow output down for this example
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // state change handled next cycle
                }
    
                System.out.println(this + " cycle " + i);
                i++;
            }
            System.out.println(this + " finished");
            // cleanup
        }
    
        public synchronized void end() {
            _state = STATE_STOP;
            try {
                this.interrupt();
                this.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public synchronized void pause() {
            _state = STATE_PAUSE;
        }
    
        public synchronized void unpause() {
            _state = STATE_RUN;
            synchronized (this) {
                this.notify();
            }
        }
    }
    

    使用
    this.wait()
    而不是
    yield()
    ,并将
    this.notify()
    添加到
    unpause()
    stop()
    中,可能是它的重复。
    class MyThread extends Thread {
        private final static int STATE_RUN = 0, STATE_PAUSE = 2, STATE_STOP = 3;
        private int _state;
    
        private static MyThread thread;
    
        public static MyThread getInstance() {
            if (thread == null || !thread.isAlive()) {
                thread = new MyThread();
            }
            return thread;
        }
    
    
        private MyThread() {
            _state = STATE_RUN;
        }
    
        public static void main(String[] args) {
            MyThread t = MyThread.getInstance();
            try {
                t.start();
                Thread.sleep(500);
                t.pause();
                Thread.sleep(500);
                t.unpause();
                Thread.sleep(500);
                t.end();
            } catch (InterruptedException e) {
                // ignore; this is just an example
            }
        }
    
        public void run() {
            int i = 0;
            while (_state != STATE_STOP) {
                if (_state == STATE_PAUSE) {
                    System.out.println(this + " paused");
                    synchronized (this) {
                        try {
                            this.wait();
                        } catch (InterruptedException e) {
                        }
                    }
                }
                if (_state == STATE_STOP) {
                    break;
                }
    
                // this is where the actual processing happens
                try {
                    // slow output down for this example
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // state change handled next cycle
                }
    
                System.out.println(this + " cycle " + i);
                i++;
            }
            System.out.println(this + " finished");
            // cleanup
        }
    
        public synchronized void end() {
            _state = STATE_STOP;
            try {
                this.interrupt();
                this.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public synchronized void pause() {
            _state = STATE_PAUSE;
        }
    
        public synchronized void unpause() {
            _state = STATE_RUN;
            synchronized (this) {
                this.notify();
            }
        }
    }