{"id":1791,"date":"2025-02-13T11:26:37","date_gmt":"2025-02-13T03:26:37","guid":{"rendered":"https:\/\/note.ui11.cc\/?p=1791"},"modified":"2025-02-13T11:26:38","modified_gmt":"2025-02-13T03:26:38","slug":"timeunit%e7%b1%bb%e4%bd%bf%e7%94%a8%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/note.ui11.cc\/index.php\/2025\/02\/13\/timeunit%e7%b1%bb%e4%bd%bf%e7%94%a8%e8%af%b4%e6%98%8e\/","title":{"rendered":"TimeUnit\u7c7b\u4f7f\u7528\u8bf4\u660e"},"content":{"rendered":"\n<p><code>TimeUnit.SECONDS<\/code>\u00a0\u662f Java \u4e2d\u00a0<code>java.util.concurrent.TimeUnit<\/code>\u00a0\u679a\u4e3e\u7c7b\u7684\u4e00\u4e2a\u5e38\u91cf\uff0c\u8868\u793a\u65f6\u95f4\u5355\u4f4d\u4e3a\u79d2\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.&nbsp;<code>TimeUnit<\/code>&nbsp;\u7c7b\u6982\u8ff0<\/h3>\n\n\n\n<p><code>TimeUnit<\/code>&nbsp;\u662f Java \u63d0\u4f9b\u7684\u4e00\u4e2a\u679a\u4e3e\u7c7b\uff0c\u4f4d\u4e8e&nbsp;<code>java.util.concurrent<\/code>&nbsp;\u5305\u4e2d\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u7ec4\u5e38\u7528\u7684\u65f6\u95f4\u5355\u4f4d\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u5728\u4e0d\u540c\u65f6\u95f4\u5355\u4f4d\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u4ee5\u53ca\u5728\u6d89\u53ca\u65f6\u95f4\u76f8\u5173\u7684\u64cd\u4f5c\u4e2d\u6307\u5b9a\u65f6\u95f4\u7684\u5ea6\u91cf\u5355\u4f4d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.&nbsp;<code>TimeUnit<\/code>&nbsp;\u5305\u542b\u7684\u65f6\u95f4\u5355\u4f4d\u5e38\u91cf<\/h3>\n\n\n\n<p><code>TimeUnit<\/code>&nbsp;\u679a\u4e3e\u7c7b\u4e2d\u5305\u542b\u4e86\u4ee5\u4e0b\u5e38\u89c1\u7684\u65f6\u95f4\u5355\u4f4d\u5e38\u91cf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>NANOSECONDS<\/code>\uff1a\u7eb3\u79d2\uff0c1 \u7eb3\u79d2\u7b49\u4e8e 10\u207b\u2079 \u79d2\u3002<\/li>\n\n\n\n<li><code>MICROSECONDS<\/code>\uff1a\u5fae\u79d2\uff0c1 \u5fae\u79d2\u7b49\u4e8e 10\u207b\u2076 \u79d2\u3002<\/li>\n\n\n\n<li><code>MILLISECONDS<\/code>\uff1a\u6beb\u79d2\uff0c1 \u6beb\u79d2\u7b49\u4e8e 10\u207b\u00b3 \u79d2\u3002<\/li>\n\n\n\n<li><code>SECONDS<\/code>\uff1a\u79d2\u3002<\/li>\n\n\n\n<li><code>MINUTES<\/code>\uff1a\u5206\u949f\uff0c1 \u5206\u949f\u7b49\u4e8e 60 \u79d2\u3002<\/li>\n\n\n\n<li><code>HOURS<\/code>\uff1a\u5c0f\u65f6\uff0c1 \u5c0f\u65f6\u7b49\u4e8e 3600 \u79d2\u3002<\/li>\n\n\n\n<li><code>DAYS<\/code>\uff1a\u5929\uff0c1 \u5929\u7b49\u4e8e 86400 \u79d2\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.&nbsp;<code>TimeUnit.SECONDS<\/code>&nbsp;\u7684\u4f7f\u7528\u573a\u666f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u5728 Redis \u64cd\u4f5c\u4e2d\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/h4>\n\n\n\n<p>\u5728\u4f7f\u7528&nbsp;<code>RedisTemplate<\/code>&nbsp;\u64cd\u4f5c Redis \u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>expire<\/code>&nbsp;\u65b9\u6cd5\u4e3a\u952e\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c\u901a\u8fc7&nbsp;<code>TimeUnit.SECONDS<\/code>&nbsp;\u6765\u6307\u5b9a\u65f6\u95f4\u5355\u4f4d\u4e3a\u79d2\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.stereotype.Service;\nimport java.util.concurrent.TimeUnit;\n\n@Service\npublic class RedisService {\n\n    @Autowired\n    private RedisTemplate&lt;String, String> redisTemplate;\n\n    public void setValueWithExpiration(String key, String value, long timeout) {\n        \/\/ \u8bbe\u7f6e\u952e\u503c\u5bf9\n        redisTemplate.opsForValue().set(key, value);\n        \/\/ \u4e3a\u952e\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\n        redisTemplate.expire(key, timeout, TimeUnit.SECONDS);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>setValueWithExpiration<\/code>&nbsp;\u65b9\u6cd5\u7528\u4e8e\u8bbe\u7f6e\u952e\u503c\u5bf9\uff0c\u5e76\u4e3a\u8be5\u952e\u8bbe\u7f6e\u4e86\u6307\u5b9a\u79d2\u6570\u7684\u8fc7\u671f\u65f6\u95f4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ebf\u7a0b\u4f11\u7720<\/h4>\n\n\n\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>TimeUnit<\/code>&nbsp;\u6765\u8ba9\u7ebf\u7a0b\u4f11\u7720\u6307\u5b9a\u7684\u65f6\u95f4\uff0c\u4f7f\u7528&nbsp;<code>TimeUnit.SECONDS<\/code>&nbsp;\u8868\u793a\u4f11\u7720\u7684\u65f6\u95f4\u5355\u4f4d\u4e3a\u79d2\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.concurrent.TimeUnit;\n\npublic class ThreadSleepExample {\n    public static void main(String&#91;] args) {\n        try {\n            System.out.println(\"\u7ebf\u7a0b\u5f00\u59cb\u4f11\u7720\");\n            \/\/ \u7ebf\u7a0b\u4f11\u7720 5 \u79d2\n            TimeUnit.SECONDS.sleep(5);\n            System.out.println(\"\u7ebf\u7a0b\u4f11\u7720\u7ed3\u675f\");\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>TimeUnit.SECONDS.sleep(5)<\/code>&nbsp;\u8ba9\u5f53\u524d\u7ebf\u7a0b\u4f11\u7720 5 \u79d2\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/h3>\n\n\n\n<p><code>TimeUnit<\/code>&nbsp;\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u7528\u4e8e\u5728\u4e0d\u540c\u65f6\u95f4\u5355\u4f4d\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u4f8b\u5982\u5c06\u79d2\u8f6c\u6362\u4e3a\u6beb\u79d2\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.concurrent.TimeUnit;\n\npublic class TimeUnitConversionExample {\n    public static void main(String&#91;] args) {\n        long seconds = 5;\n        \/\/ \u5c06\u79d2\u8f6c\u6362\u4e3a\u6beb\u79d2\n        long milliseconds = TimeUnit.SECONDS.toMillis(seconds);\n        System.out.println(seconds + \" \u79d2\u7b49\u4e8e \" + milliseconds + \" \u6beb\u79d2\");\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>TimeUnit.SECONDS\u00a0\u662f Java \u4e2d\u00a0java.util.concurrent.TimeUnit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,17],"tags":[],"class_list":["post-1791","post","type-post","status-publish","format-standard","hentry","category-redis","category-17"],"_links":{"self":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1791"}],"version-history":[{"count":1,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1791\/revisions"}],"predecessor-version":[{"id":1792,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1791\/revisions\/1792"}],"wp:attachment":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}