博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
_thread_in_vm_Java Thread类的静态void sleep(long time_in_ms,int time_in_ns)方法,带示例
阅读量:2528 次
发布时间:2019-05-11

本文共 4374 字,大约阅读时间需要 14 分钟。

_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.javaE:\Programs>java MainThread started:mainThread started:Thread-0Thread started:mainThread started:mainThread started:mainThread started:mainThread ended:mainThread started:Thread-0Thread Ended :Thread-0

翻译自:

_thread_in_vm

转载地址:http://ayxzd.baihongyu.com/

你可能感兴趣的文章
使用css的类名交集复合选择器 《转》
查看>>
[USACO18DEC]The Cow Gathering
查看>>
情感分析-英文电影评论
查看>>
王者荣耀游戏服务器架构的演进读后感
查看>>
关于ajax请求controller返回中文乱码的解决方法!
查看>>
Objective-C 和 Core Foundation 对象相互转换的内存管理总结
查看>>
IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE...
查看>>
Linux nmon 命令
查看>>
使用 urllib 构造请求对象
查看>>
sql server book
查看>>
长亭技术专栏 安全攻防技术分享
查看>>
sql server dba
查看>>
visualvm
查看>>
docker(4):coreos+docker+rancher真厉害
查看>>
设计模式之代理模式,学习笔记
查看>>
在Qsys中创建用户自定义IP
查看>>
【leetcode】Container With Most Water
查看>>
Python -- machine learning, neural network -- PyBrain 机器学习 神经网络
查看>>
一道dp题目
查看>>
mysql中间件研究(tddl atlas cobar sharding-jdbc)
查看>>