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 FuncThread extends Thread { private final String name; public FuncThread(String name) { this.name = name; } @Override public void run() { System.out.println(String.format("%s is running", name)); try { Thread.sleep(Math.abs(new Random().nextInt(10-2)+2) * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(String.format("%s ends", name)); } }

Questions about programming?Chat with your personal AI assistant