Java的Thread.sleep()方法用于让当前线程暂停执行一段时间。它有两种使用方式:
使用固定的时间间隔:try { // 暂停执行500毫秒(0.5秒) Thread.sleep(500);} catch (InterruptedException e) { e.printStackTrace();}使用动态的时间间隔:try { // 暂停执行一段时间,时间间隔为动态变量milliseconds long milliseconds = 1000; Thread.sleep(milliseconds);} catch (InterruptedException e) { e.printStackTrace();}注意:Thread.sleep()方法会抛出一个InterruptedException异常,这意味着如果线程在休眠期间被中断,会抛出该异常。因此,在使用Thread.sleep()方法时,通常需要在try-catch块中处理这个异常。

