Generation

generate functionWed, 28 Jun 2023

スレッド側 スレッドのIDを取得,開始時刻を取得,乱数で2から10秒待ち, 終了時刻を取得し,スレッドID,開始時刻,待ち秒数,終了時刻を表⽰ 参考 スレッドID取得 Thread.currentThread().getId() 現在時間取得 java.time.LocalTimeのLocalTime.now() 時刻書式指定 java.time.format.DateTimeFormatterのofPattern()など 整数値nまでの乱数 randomインスタンスのnextInt(n) 整形して表⽰ System.out.printf( 書式指定,表⽰項⽬ )

public class Thread1 extends Thread { @Override public void run() { long id = Thread.currentThread().getId(); LocalTime start = LocalTime.now(); int wait_sec = new Random().nextInt(9) + 2; try { sleep(wait_sec * 1000); } catch (InterruptedException e) { e.printStackTrace(); } LocalTime end = LocalTime.now(); System.out.printf("id:%d, start:%s, wait:%d, end:%s%n", id, start.format(DateTimeFormatter.ofPattern("mm:ss.SSS")), wait_sec, end.format(DateTimeFormatter.ofPattern("mm:ss.SSS"))); } }

Questions about programming?Chat with your personal AI assistant