请完成下列Java程序:运行3个线程,每一个线程有自己的标志,用a,b,c表示,每个线程显示一个“Start”信息和一个“End”信息并且间隔地显示2个“Loop”信息(间隔变化为(0.5-2)秒之间的随机延迟)。

程序运行结果如下:(注:由于时间间隔为随机数,所以,运行结果的顺序不惟一) a Start b Start c Start b Loop a Loop b Loop b End c Loop a Loop a End c Loop c End 注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。 public class ex2_2 implements Runnable { static char flag2_2 = 'a'; public static void main(String[] arg) { ex2_2 obj2_2 = new ex2_2(); Thread thread2_2 = new Thread(obi2_2); thread2_2.start(); thread2_2 = new Thread(obj2_2); thread2 2.start(); thread2_2 = new Thread(obi2_2); thread2_2, start (); } public void run() { char myflag2_2; synchronized(this) { _________________; } System.out.println(myflag2_2 +" Start"); for(int i=0; i<2; i++) { try { Thread.sleep(rand(500,2000)); System.out.println(myflag2_2 +" Loop"); } catch(InterruptedException ie) { System.out.println(ie); } } System.out.println(myflag2_2 +" End"); } final private int rand(int low, int high) return(_________________); } }

时间:2023-09-29 16:32:13

相似题目