问答题

简单应用题 请完成下列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(obj2_2); thread2_2.start(); thread2_2 = new Thread(obj2_2); thread2_2.start(); thread2_2 = new Thread(obj2_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( ); } }

【参考答案】

myflag2_2 = flag2_2++((int)((high-low+1)*(Math.random()))) +......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
综合应用题下面是一个Applet程序,其功能是根据给出的小时,分钟和秒数计算相等的秒数,即将1分钟化为60秒,依此类推。要求建立一个时间类,时间参数均作为类的成员变量,并且给出换算时间的方法,也作为这个类的成员函数,可以供外部对象进行调用。同时还需要在输出窗口中显示换算结果,并且将结果写到out3_3.txt文件中,本题给出确定的时间为4小时23分47秒,要求换算成以秒做单位的时间。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:import java.io.*;import java.awt.*;import java.applet.*; ** public class ex3_3 extends Applet{public void paint(Graphics g){int nSum;class myTime3_3{public int h;public int m;public int s;public int out;public int caculateSecond(){out = h*60+m*60+s;return out;}}myTime3_3 objTime3_3 = new myTime3_3();objTime3_3.h = 4;objTime3_3.m = 23;objTime3_3.s = 47;nSum = objTime3_3.caculateSecond();g.drawString ( 时: +objTime3_3.h, 20, 30);g.drawString ( 分: +objTime3_3.m, 20, 50);g.drawString ( 秒: +objTime3_3.s, 20, 70);g.drawString ( 合计: +objTime3_3.out+ 秒 , 20, 90);try {FileOutputStream fos3_3 = new FileOutputStream( out3_3.txt );BufferedOutputStream bos3_3=new BufferedOutputStream(fos3_3,1024);PrintStream ps3_3=new PrintStream(bos3_3,false);System.setOut(ps3_3);System.out.println( 合计: +objTime3_3.out+ 秒 );ps3_3.close();} catch(IOException ioe) {System.out.println(ioe);}}}ex3_3.htmlex3_3