Java Object类的最终void wait(long ms,int ns)方法,带示例

对象类最终无效等待(long ms,int ns) (Object Class final void wait(long ms , int ns))

  • This method is available in java.lang.Object.wait(long ms, int ns).

    此方法在java.lang.Object.wait(long ms,int ns)中可用。

  • This method causes the current thread to wait for a specified amount of time in milliseconds and nanoseconds until another thread notification by calling notify() or notifyAll() method of the object.

    此方法使当前线程等待指定的时间(以毫秒和纳秒为单位),直到通过调用对象的notify()或notifyAll()方法发出另一个线程通知为止。

  • This method throws an InterruptedException when other thread interrupted current thread.

    当其他线程中断当前线程时,此方法将引发InterruptedException 。

  • This method can't override because it is final.

    此方法是最终方法,因此无法覆盖。

  • The time will be given in the method is of milliseconds and nanoseconds.

    该方法将给出的时间为毫秒和纳秒。

Syntax:

句法:

    final void wait(long ms, int ns){
    }

Parameter(s):

参数:

Here we are passing two-time parameter one is in milliseconds and another is in nanoseconds(How long a thread has to wait i.e. we have to mention time in milliseconds and if want extra time then we can mention time in nanoseconds as well) as a parameter in the method of the Object class.

在这里,我们传递了两个时间参数,一个以毫秒为单位,另一个以纳秒为单位(线程必须等待多长时间,即我们必须以毫秒为单位提及时间,如果需要额外的时间,那么我们也可以以纳秒为单位提及时间) Object类方法中的参数。

Return value:

返回值:

The return type of this method is void that means this method returns nothing after execution.

该方法的返回类型为void ,这意味着该方法在执行后不返回任何内容。

Java程序演示对象类的wait(long ms,int ns)方法的示例 (Java program to demonstrate example of Object Class wait(long ms , int ns) method)

import java.lang.Object;

public class Thread1 {
    public static void main(String[] args) throws InterruptedException {

        // Create an object of Thread2

        Thread2 t2 = new Thread2();

        // By calling start() so Thread start() will exceute
        t2.start();

        Thread.sleep(1000);

        synchronized(t2) {

            System.out.println("Main thread trying to call wait()");

            // By calling wait() causes the current thread to wait 
            // for 1000 milliseconds until another   thread notification
            t2.wait(1000);

            System.out.println("Main Thread get notification here");
            System.out.println(t2.total);

        }

    }
}

class Thread2 extends Thread {

    int total = 0;

    public void run() {

        synchronized(this) {

            System.out.println("Thread t2 starts notification");

            for (int i = 0; i < 50; ++i) {

                total = total + i;
            }

            System.out.println("Thread t2 trying to given notification");

            this.notify();

        }

    }
}

Output

输出量

D:\Programs>javac Thread1.java

D:\Programs>java Thread1
Main thread trying to call wait()
Thread t2 starts notification
Thread t2 trying to given notification
Main Thread get notification here
190


翻译自: https://www.includehelp.com/java/object-class-final-void-wait-long-ms-int-ns-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值