_thread_in_vm_Java Thread类的静态void sleep(long time_in_ms,int time_in_ns)方法,带示例

_thread_in_vm

线程类静态无效睡眠(long time_in_ms,int time_in_ns) (Thread Class static void sleep(long time_in_ms, int time_in_ns))

  • This method is available in package java.lang.Thread.sleep(long time_in_ms, int time_in_ns).

    软件包java.lang.Thread.sleep(long time_in_ms,int time_in_ns)中提供了此方法。

  • sleep(long time_in_ms, int time_in_ns) method is applicable when we want to stop current executing thread for particular amount of time in milliseconds + nanoseconds (i.e. with additional time in nanoseconds) as well or in other words if a thread causes current thread to stop executing for some time in milliseconds + nanoseconds given in the method.

    sleep(long time_in_ms,int time_in_ns)方法适用于当我们想要以毫秒+纳秒(即,额外的时间以纳秒为单位)的特定时间量停止当前正在执行的线程时,或者换句话说,如果线程导致当前线程停止该方法执行的时间以毫秒+纳秒为单位。

  • This method is static so we can access this method with the class name too.

    该方法是静态的,因此我们也可以使用类名访问此方法。

  • The return type of this method is void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

  • This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get a compile-time error.

    该方法抛出InterruptedException异常,因此需要通过try-catch或throws来处理异常,否则我们将获得编译时错误。

  • We pass here two parameters in the given method of the Thread class and the parameter will be time_in_ms(time in milliseconds) and time_in_ns(time in nanoseconds) this two-parameter is the duration of time to sleep of our Thread so this thread wait for ms+ns time.

    我们在此处通过Thread类的给定方法传递两个参数,该参数将是time_in_ms(以毫秒为单位的时间)和time_in_ns(以纳秒为单位的时间),这两个参数是线程Hibernate的持续时间,因此该线程等待ms + ns时间。

  • If other thread takes less time to executes so in that case if we call sleep() method then there is a possibility of completion of the Thread because the current thread will wait for time_in_ms + time_in_ms.

    如果其他线程执行的时间较少,那么在这种情况下,如果我们调用sleep()方法,则有可能完成该线程,因为当前线程将等待time_in_ms + time_in_ms 。

For example, We have two threads [t1 – MyThread], [t2 – main] so will see what will happen.

例如,我们有两个线程[ t1 – MyThread],[ t2 – main],因此将看到会发生什么。

Let suppose if a thread t1 executes and in the meanwhile if we call sleep(1000,500) method like this /* Thread.sleep(1000,500)*/ inside MyThread so this thread will stop its execution for 1000 millisecond and 500 nanoseconds will wait for the processor and if the thread allocates processor again then the same thread will continue its execution.

假设是否执行了线程t1,同时在MyThread中调用了像这样的sleep(1000,500)方法/ * Thread.sleep(1000,500)* /,那么该线程将在1000毫秒和500纳秒内停止执行将等待处理器,并且如果线程再次分配处理器,则同一线程将继续执行。

Syntax:

句法:

    static void sleep(long time_in_ms, int time_in_ns){
}

Parameter(s):

参数:

When we write Thread.sleep(2000,1000) so this line means currently executing thread will stop its execution for 2000 milliseconds and additional 1000 nanoseconds we need to remember the same thread will stop its execution from where sleep() method is called.

当我们编写Thread.sleep(2000,1000)时 ,此行表示当前正在执行的线程将在2000毫秒内停止执行,另外1000纳秒,我们需要记住同一线程将在调用sleep()方法的位置停止执行。

Return value:

返回值:

The return type of this method is void, it does not return anything.

此方法的返回类型为void ,它不返回任何内容。

Java程序,演示sleep()方法的示例 (Java program to demonstrate example of sleep() method)

/*  We will use Thread class methods so we are importing 
the package but it is not mandate because 
it is imported by default
*/
import java.lang.Thread;
class MyThread extends Thread {
//Override run() method of Thread class 
public void run() {
for (int i = 0; i < 2; ++i) {
System.out.println("Thread started:" + Thread.currentThread().getName());
try {
Thread.sleep(1000, 500);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
System.out.println("Thread Ended :" + Thread.currentThread().getName());
}
}
class MainThread1 {
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
mt.start();
for (int j = 0; j < 5; ++j)
System.out.println("Thread started:" + Thread.currentThread().getName());
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}

Output

输出量

E:\Programs>javac Main.java
E:\Programs>java Main
Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:main
Thread started:main
Thread started:main
Thread ended:main
Thread started:Thread-0
Thread Ended :Thread-0

翻译自: https://www.includehelp.com/java/thread-class-static-void-sleep-long-time_in_ms-int-time_in_ns-method-with-example.aspx

_thread_in_vm

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/376968.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大规模web服务开发技术(转)

前段时间趁空把《大规模web服务开发技术》这本书看完了&#xff0c;今天用一下午时间重新翻了一遍&#xff0c;把其中的要点记了下来&#xff0c;权当复习和备忘。由于自己对数据压缩、全文检索等还算比较熟&#xff0c;所以笔记内容主要涉及前5章内容&#xff0c;后面的零星记…

IO多路复用的三种机制Select,Poll,Epoll

IO多路复用的本质是通过系统内核缓冲IO数据让单个进程可以监视多个文件描述符&#xff0c;一旦某个进程描述符就绪(读/写就绪)&#xff0c;就能够通知程序进行相应的读写操作。 select poll epoll都是Linux提供的IO复用方式&#xff0c;它们本质上都是同步IO&#xff0c;因为它…

qt中按钮贴图

一.QT之QPushButton按钮贴图 二.QT之QToolButton按钮贴图 一.QT之QPushButton按钮贴图具体操作流程 1. Qt Designer中拖入一Tool Button 2. 选择图标的图片放入工程目录下&#xff0c;如放在Resources内 3. 双击工程的Resource Files下的qrc文件&#xff0c;如图 4. 在弹出的窗…

Ubuntu手动编译gVim7.3修复终端启动时与ibus的冲突

个bug伴随着Ubuntu/ibus的升级苦憋已久&#xff0c;症状为终端启动gvim时卡死&#xff0c;gvim -f可以缓解此问题&#xff0c;但偶尔还是要发作&#xff0c;况且每次末尾托个&也不方便。其实新版gvim已经修复此bug&#xff0c;不过ubuntu安装包一直没更新&#xff0c;那我们…

Android Activity类讲解(一)

--by CY[kotomifigmail.com] &#xff11;&#xff0e;protected void onCreate(Bundle savedInstanceState) { throw new RuntimeException("Stub!");   } 当创建一个Activity时&#xff0c;系统会自动调用onCreate方法来完成创建工作&#xff0e;该创建工作包括布…

Mysql的undo、redo、bin log分析

目录关于undo log关于redolog关于binlog一个事务的提交流程undo log :记录数据被修改之前的样子 redo log&#xff1a;记录数据被修改之后的样子 bin log&#xff1a;记录整个操作。 关于undo log 关于undo log&#xff1a; 在执行一条涉及数据变更的sql时&#xff0c;在数据…

typedef 字符串_typedef在C中使用字符数组(定义别名来声明字符串)的示例

typedef 字符串Here, we have to define an alias for a character array with a given number of maximum characters length to read strings? 在这里&#xff0c;我们必须为具有给定最大字符长度数的字符数组定义别名&#xff0c;以读取字符串 &#xff1f; In the below-…

最小堆实现代码

参考算法导论、数据结构相关书籍&#xff0c;写得最小堆实现的源代码如下&#xff1a; 1 //2 //--最小堆实例3 //4 5 #include <iostream>6 #include <vector>7 #include <string>8 using namespace std;9 10 template<typename Comparable>11 class m…

非常好的在网页中显示pdf的方法

今天有一需求&#xff0c;要在网页中显示pdf&#xff0c;于是立马开始搜索解决方案&#xff0c;无意中发现一个非常好的解决方法&#xff0c;详见http://blogs.adobe.com/pdfdevjunkie/web_designers_guide。 其实就光看这个网站也足够了&#xff0c;http://www.pdfobject.com/…

Redis字典实现、Hash键冲突以及渐进式rehash

本笔记参考《Redis设计与实现》 P24~ 37 目录Redis字典实现哈希表节点结构哈希表结构字典哈希算法解决hash冲突rehash渐进式hashRedis字典实现 哈希表节点结构 typedef struct dictEntry {// 键void *key;// 值 : 可以是一个指针&#xff0c;或者是一个uint64/int64 的整数un…

Java线程类void setContextClassLoader(ClassLoader loader)方法,带示例

线程类void setContextClassLoader(ClassLoader loader) (Thread Class void setContextClassLoader(ClassLoader loader)) This method is available in package java.lang.Thread.setContextClassLoader(ClassLoader loader). 软件包java.lang.Thread.setContextClassLoader(…

JPA概要

本文最新版已更新至&#xff1a;http://thinkinside.tk/2012/12/30/JPA.html JPA定义了Java ORM及实体操作API的标准。本文摘录了JPA的一些关键信息以备查阅。 如果有hibernate的基础&#xff0c;通过本文也可以快速掌握JPA的基本概念及使用。 Table of Contents 1 JPA概述2 实…

如何配置能让fiddler抓去https的请求?

1、打开fiddler&#xff0c;>>Tools>>Fiddler Options&#xff0c; 打开如图所示的HTTPS配置项&#xff1a;点击Export Rppt Certifica to Desktop :桌面上多了一个证书&#xff1a;下面就是将证书导入&#xff1a;点击开始-运行&#xff0c;输入&#xff1a;mmc,…

Redis对象的refcount与lru属性(内存回收、对象共享、空转时长)

本笔记参考《Redis设计与实现》 P84~P88 内存回收 Redis在对象系统中使用reference counting技术实现了内存回收机制。程序可以通过跟踪对象的引用计数信息&#xff0c;在适当的时候自动释放对象并进行内存回收。 typedef struct redisObject {// ...// 引用计数int refcoun…

【闲聊】Baidu Map,excellent !!!Diaoyv island is China 's

【钓鱼岛】钓鱼岛是中国的&#xff01;Diaoyu Islands are Chinas! 釣魚島は中国のアール! ————————————youngLaker转载于:https://www.cnblogs.com/younglaker/archive/2012/12/31/2840190.html

08:vigenère密码_密码技术:Vigenére密码,Playfair密码,Hill密码

08:vigenre密码1)Vigenre密码 (1) Vigenre Cipher) This technique is an example of Polyalphabetic Substitution technique which uses 26 Caesar ciphers make up the mono-alphabetic substitution rules which follow a count shifting mechanism from 0 to 25. That is,…

Redis的RDB文件与AOF文件

本笔记参考《Redis设计与实现》 P118 ~ P150 RDB文件 1、RDB文件用于保存和还原Redis服务器所有数据库中的所有键值对数据 2、SAVE命令由服务器进程直接执行保存操作&#xff0c;该命令会阻塞服务器 3、BGSAVE命令由子进程执行保存操作&#xff0c;不会阻塞服务器 注意此时服…

eclipse扩容

eclipse扩容 -vmD:/jdk-6u17-windows-i586/jdk1.6.0_17/bin/javaw.exe-startupplugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar-nlen_US--launcher.libraryplugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.200.v20120913-144807-productorg.eclipse…

node oauth2验证_如何设置和使用护照OAuth Facebook身份验证(第2部分)| Node.js

node oauth2验证In my last article (How to set up and use passport OAuth Facebook Authentication (Section 1) | Node.js), we looked at another form of authentication called the OAuth authentication which involves sign in or signup using social media. 在我的上…

Python and Microsoft Word

国外网站看到的文章&#xff1a;Accessing Microsoft Word with Python follows the same syntax that we used for Excel. Let’s take a quick look at how to access Word.from time import sleep import win32com.client as win32RANGE range(3, 8)def word():word win32…