第十章:多线程处理与JAVA网络编程概念.docVIP

  • 1
  • 0
  • 约 18页
  • 2017-05-17 发布于湖北
  • 举报

第十章:多线程处理与JAVA网络编程概念.doc

第九章:流式输出输入与多线程 EX1:(题库第九章ex2) 难度等级:A 使用XXX方法来优化线程的使用,提高多线程中的线程灵活性 /*这是一个关于多线程的实例,这个程序首先继承了Thread类,并且重载了方法run()用来演示线程的具体的工作过程*/ public class SimpleThread1 extends Thread { private int i=5; private int intThreadNumber; //整型变量,用来标记当前线程的编号 private static int intThreadCount=0; //静态变量,用来计算启动的线程的个数 public SimpleThread1() { intThreadCount++; //设置线程的个数和当前线程的编号 intThreadNumber=intThreadCount; //打印提示信息 System.out.println(Making Thread+intThreadNumber); } //重载Thread类的方法,该过程打印线程的编号和当前执行的次数 public void run() { while(i0) { System.out.println (Thread +intThreadNumber+ i= +i); i--; //i的变化 yield(); //调用函数,主动放弃对线程的控制,让出CPU资源 } } public static void main(String[] args) { for(int i=0;i5;i++) { new SimpleThread1().start(); //线程开始 } System.out.println (All Thread Started); } } EX2(JAVA题库第九章EX3): 编写一Applet程序,要求三个按钮可以控制三个独立线程的状态. 还有用三个文本区显示三个独立线程的状态import java.awt.*; import java.applet.Applet; public class RandomCharacters extends Applet implements Runnable { String alphabet; TextField output1,output2,output3; Button button1,button2,button3; Thread thread1,thread2,thread3; boolean suspend1,suspend2,suspend3; public void init() { alphabet=new String(ABCDEFGHIJKLMNOPQRSTUVWXYZ); output1=new TextField(10); output1.setEditable (false); output2=new TextField(10); output2.setEditable (false); output3=new TextField(10); output3.setEditable (false); button1=new Button(Suspend/Resume 1); button2=new Button(Suspend/Resume 2); button3=new Button(Suspend/Resume 3); add(output1); add(button1); add(output2); add(button2); add(output3); add(button3); } public void start() { //create threads and start every time start is called thread1=new Thread( this,Thread 1); thread2=new Thread( this,Thread 2); thread3=new Thread( this,Thread 3); thread1.start (); thread2.start (); thread3.start (); } public void stop() { //stop threads every time stop is called //as the user browses anothe

文档评论(0)

1亿VIP精品文档

相关文档