SystemClock.sleep和Thread.sleep的区别

 

在Java中我们处理线程同步问题时,处理延迟可能会使用Thread类的sleep方法,这里抛开concurrent类的一些方法,其实 Android平台还提供了一个SystemClock.sleep方法,它们有什么区别呢?

我们每次调用Thread.sleep时可能会出现InterruptedException异常,而SystemClock.sleep方法则不会,在 SDK上有这样的描述,它将会忽略中断异常。

Three different clocks are available, and they should not be confused:

  • System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.

  • uptimeMillis() is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls), Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic, and is the recommended basis for the general purpose interval timing of user interface events, performance measurements, and anything else that does not need to measure elapsed time during device sleep. Most methods that accept a timestamp value expect the uptimeMillis() clock.

  • elapsedRealtime() is counted in milliseconds since the system was booted, including deep sleep. This clock should be used when measuring time intervals that may span periods of system sleep.

There are several mechanisms for controlling the timing of events:

 

在C++中创建一个简单的时钟和闹钟程序,我们可以将功能分为两个类:一个基础时钟(Clock),用于显示当前时间;另一个闹钟(RingClock),在特定时间触发提醒。这里是一个基本的框架结构: 首先,我们有两个头文件 Clock.h 和 RingClock.h,分别包含各自的类声明。 **Clock.h** ```cpp #ifndef CLOCK_H #define CLOCK_H #include <iostream> #include <ctime> class Clock { public: Clock(); void displayTime(); private: std::time_t currentTime; }; #endif // CLOCK_H ``` **Clock.cpp** ```cpp #include "Clock.h" Clock::Clock() { currentTime = std::time(nullptr); } void Clock::displayTime() { std::cout << "Current Time: " << std::ctime(&currentTime) << std::endl; } ``` **RingClock.h** ```cpp #ifndef RINGCLOCK_H #define RINGCLOCK_H #include "Clock.h" #include <string> #include <chrono> class RingClock { public: RingClock(const std::string& alarmTime, const Clock& clock); void setAlarm(const std::string& alarmTime); private: std::string alarmTimeString; Clock clockObj; std::chrono::time_point<std::chrono::system_clock> alarmTimePoint; }; #endif // RINGCLOCK_H ``` **RingClock.cpp** ```cpp #include "RingClock.h" RingClock::RingClock(const std::string& alarmTime, const Clock& clock) : alarmTimeString(alarmTime), clockObj(clock) { if (!parseAlarmTime(alarmTimeString)) { throw std::invalid_argument("Invalid alarm time format"); } alarmTimePoint = std::chrono::system_clock::from_time_t(std::mktime(std::strptime(alarmTimeString.c_str(), "%H:%M"))); } bool RingClock::parseAlarmTime(const std::string& alarmTimeString) { struct tm time_struct; return std::strptime(alarmTimeString.c_str(), "%H:%M", &time_struct) != nullptr && std::mktime(&time_struct) != -1; } void RingClock::setAlarm(const std::string& alarmTime) { alarmTimeString = alarmTime; // 实现闹钟定时功能,例如,使用`std::thread`和条件变量 // ... } // 假设有一个函数ringTheAlarm() 来触发闹钟,这里仅做简单示例 void RingClock::ringTheAlarm() { while (true) { auto now = std::chrono::system_clock::now(); if (now >= alarmTimePoint) { // 响铃 std::cout << "Alarm rings at: " << alarmTimeString << std::endl; break; } std::this_thread::sleep_for(std::chrono::seconds(1)); } } ``` 在 main.cpp 中,我们可以实例化这两个类并设置闹钟: **main.cpp** ```cpp #include "main.h" // Include your header files here, assuming they are in the same directory int main() { Clock clock; clock.displayTime(); RingClock ringClock("08:30", clock); // 设置8:30作为闹钟时间 ringClock.setAlarm("08:30"); // 设置闹钟 // 开启环形闹钟线程 std::thread ringThread(ringClock.ringTheAlarm); // 主线程等待闹钟响铃 ringThread.join(); return 0; } ``` 这个例子展示了如何在C++中创建一个简单的时钟和闹钟程序的基本架构。实际应用中,你需要完善闹钟的定时部分,并可能添加更多的功能,如取消闹钟等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值