博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java void方法_Java对象类的最终void wait(long ms)方法,包含示例
阅读量:2531 次
发布时间:2019-05-11

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

java void方法

对象类最终无效等待(长毫秒) (Object Class final void wait(long ms))

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

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

  • This method causes the current thread to wait for a specified amount of time 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.

    该方法将给出的时间以毫秒为单位。

Syntax:

句法:

final void wait(long ms){    }

Parameter(s):

参数:

We can pass an object(How long a thread has to wait i.e. we have to mention time in milliseconds) 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程序演示对象类等待(长毫秒)方法的示例 (Java program to demonstrate example of Object Class wait(long ms) 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.javaD:\Programs>java Thread1Thread t2 starts notificationThread t2 trying to given notificationMain thread trying to call wait()Main Thread get notification here1225

翻译自:

java void方法

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

你可能感兴趣的文章
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>