首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尽管使用了睡眠,但计时错误

尽管使用了睡眠,但计时错误
EN

Stack Overflow用户
提问于 2011-11-10 22:28:45
回答 1查看 2.4K关注 0票数 0

我在一个IOIO Android开发板上工作。我正在试着控制两个引脚和一个发光二极管,并且每隔一段时间就把它们打开。到目前为止,它连接得很好,发光二极管和引脚都是开着和关着的,但时机不对。我认为问题出在sleep()函数上。任何帮助或指导都将不胜感激。

代码语言:javascript
复制
                 class IOIOThread extends Thread {
        private IOIO ioio_;
        private boolean abort_ = false;


        @Override
        public void run() {
            super.run();
            while (true) {
                synchronized (this) {
                    if (abort_) {
                        break;
                    }
                    ioio_ = IOIOFactory.create();
                }
                try {
                    setText(R.string.wait_ioio);
                    ioio_.waitForConnect();
                    setText(R.string.ioio_connected);

                    while (true) {

                        while((button_.isChecked())==true){

                            DigitalOutput led = ioio_.openDigitalOutput(0,   false);;
                            DigitalOutput S1 = ioio_.openDigitalOutput(35,   false);
                            DigitalOutput S2 = ioio_.openDigitalOutput(36,  false);
                            DigitalOutput S3 = ioio_.openDigitalOutput(37,   false);
                            DigitalOutput S4 = ioio_.openDigitalOutput(38,   false);

                            holdTime=30000;

                            for (currentTime=0; currentTime<=holdTime;)
                            {
                                led.write(true);
                                S1.write(true);
                                S2.write(true);
                                sleep(4000);
                                led.write(false);
                                S1.write(false);
                                S2.write(false);
                                sleep(2000);
                                led.write(true);
                                S3.write(true);
                                S4.write(true);
                                sleep(4000);
                                led.write(false);
                                S3.write(false);
                                S4.write(false);
                                sleep(4000);
                                currentTime = currentTime+14000;

                        }}}}

                catch (ConnectionLostException e) {
                } catch (Exception e) {
                    Log.e("HelloIOIOPower", "Unexpected exception caught", e);
                    ioio_.disconnect();
                    break;
                } finally {
                    try {
                        ioio_.waitForDisconnect();
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-10 22:52:59

Sleep()并不是实时的--它只会让你的线程保持至少指定的毫秒数。在此之后,它可用于调度,并将在某个时候执行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8081042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档