第九章:流式输出输入与多线程EX1:(题库第九章ex2)难度等级:A使用XXX方法来优化线程的使用,提高多线程中的线程灵活性/*这是一个关于多线程的实例,这个程序首先继承了Thread类,并且重载了方法run()用来演示线程的具体的工作过程*/publicclassSimpleThread1extendsThread{privateinti=5;privateintintThreadNumber;//整型变量,用来标记当前线程的编号privatestaticintintThreadCount=0;//静态变量,用来计算启动的线程的个数publicSimpleThread1(){intThreadCount++;//设置线程的个数和当前线程的编号intThreadNumber=intThreadCount;//打印提示信息System.out.println(MakingThread+intThreadNumber);}//重载Thread类的方法,该过程打印线程的编号和当前执行的次数publicvoidrun(){while(i0){System.out.println(Thread+intThreadNumber+i=+i);i--;//i的变化yield();//调用函数,主动放弃对线程的控制,让出CPU资源}}publicstaticvoidmain(String[]args){for(inti=0;i5;i++){newSimpleThread1().start();//线程开始}System.out.println(AllThreadStarted);}}EX2(JAVA题库第九章EX3):编写一Applet程序,要求三个按钮可以控制三个独立线程的状态.还有用三个文本区显示三个独立线程的状态importjava.awt.*;importjava.applet.Applet;publicclassRandomCharactersextendsAppletimplementsRunnable{Stringalphabet;TextFieldoutput1,output2,output3;Buttonbutton1,button2,button3;Threadthread1,thread2,thread3;booleansuspend1,suspend2,suspend3;publicvoidinit(){alphabet=newString(ABCDEFGHIJKLMNOPQRSTUVWXYZ);output1=newTextField(10);output1.setEditable(false);output2=newTextField(10);output2.setEditable(false);output3=newTextField(10);output3.setEditable(false);button1=newButton(Suspend/Resume1);button2=newButton(Suspend/Resume2);button3=newButton(Suspend/Resume3);add(output1);add(button1);add(output2);add(button2);add(output3);add(button3);}publicvoidstart(){//createthreadsandstarteverytimestartiscalledthread1=newThread(this,Thread1);thread2=newThread(this,Thread2);thread3=newThread(this,Thread3);thread1.start();thread2.start();thread3.start();}publicvoidstop(){//stopthreadseverytimestopiscalled//astheuserbrowsesanotherWebpagethread1.stop();thread2.stop();thread3.stop();}publicbooleanaction(Eventevent,Objectobj){if(event.target==button1)if(suspend1){thread1.resume();suspend1=false;}else{thread1.suspend();output1.setText(suspended);suspend1=true;}elseif(event.target==button2)if(suspend2){thread2.resume();suspend2=false;}else{thread2.suspend();output2.setText(suspended);suspend2=true;}elseif(event.target==button3)if(suspend3){thread3.resume();suspend3=false;}else{thread3.suspend();output3.setText(suspended);suspend3=true;}returntrue;}publicvoidrun(){intlocation;chardisplay;StringexcutingThread;while(true){//sleepfrom0to5secondstry{Thread.sleep((int)(Math.random()*5000));}catch(InterruptedExceptione){e.printStackTrace();}location=(int)(Math.random()*26);display=alphabet.charAt(location);excutingThread=Thread.currentThread().getName();if(excutingThread.equals(Thread1))output1.setText(Thread1:+display);elseif(excutingThread.equals(Thread2))output2.setText(Thread2:+display);elseif(excutingThread.equals(Thread3))output3.setText(Thread3:+display);}}}EX3:(JAVA题库第九章EX5)难度登记:B编程实现一个电子表的功能,它能不断的显示当前的时间.使用线程技术来实现/*这是一个关于线程的使用的实例,该程序实现了一个电子表的功能,并使用线程机制来实现这个功能*/importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjava.util.*;publicclassClockextendsApplet{privateClockThreadclockThread;privateButtonbtPause=newButton(Pause);privateButtonbtStart=newButton(Start);TextFieldtfTime=newTextField(35);publicvoidinit(){add(tfTime);btStart.addActionListener(newStartListener());add(btStart);btPause.addActionListener(newPauseListener());add(btPause);}//开始按钮的事件监听器classStartListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){if(clockThread==null)clockThread=newClockThread(Clock.this);}}//Pause按钮事件监听器classPauseListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){if(clockThread!=null)clockThread.InvertFlag();//调用一个自定义函数,是线程结束}}publicstaticvoidmain(String[]args){Clockapplet=newClock();FrameaFrame=newFrame(Clock);//增加windows事件处理,用来推出程序aFrame.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});aFrame.add(applet,BorderLayout.CENTER);aFrame.setSize(300,200);applet.init();applet.start();aFrame.setVisible(true);}}//用来处理电子表的线程classClockThreadextendsThread{privateClockclock;privatebooleanblRunFlag=true;//在初始化的时候就启动线程publicClockThread(Clockclock){this.clock=clock;start();//启动线程}publicvoidInvertFlag(){blRunFlag=!blRunFlag;}publicvoidrun(){while(true){try{Thread.currentThread().sleep(1000);//返回当前执行的线程对象}catch(InterruptedExceptione){}if(blRunFlag){Datetime=newDate();clock.tfTime.setText(time.toString());}}}EX4:(JAVA题库第九章EX5)通过实现Runnable接口来实现编程实现一个电子表的功能,它能不断的显示当前的时间.使用线程技术来实现/*这是一个关于线程的使用的实例,该程序实现了一个电子表的功能,程序中使用线程机制来实现这个功能*/importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjava.util.*;publicclassClockextendsAppletimplementsRunnable//填写接口名{privatebooleanblRunFlag=true;privateThreadclockThread;privateButtonbtPause=newButton(Pause);privateButtonbtStart=newButton(Start);TextFieldtfTime=newTextField(35);privatelonglngTime=newDate().getTime();publicvoidinit(){add(tfTime);btStart.addActionListener(newStartListener());add(btStart);btPause.addActionListener(newPauseListener());add(btPause);}//开始按钮的事件监听器classStartListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){if(clockThread==null