实验五 多线程程序设计_第1页
实验五 多线程程序设计_第2页
实验五 多线程程序设计_第3页
实验五 多线程程序设计_第4页
实验五 多线程程序设计_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验五多线程程序设计实验目的掌握Java语言中多线程编程的基本方法掌握Runnable接口实现多线程的方法掌握Thread类实现多线程的用法实验导读进程和线程的概念进程是程序一次动态执行的过程,对应从代码加载、执行到执行结束这样一个完整的过程,也是进程自身从产生、发展到消亡的过程。线程是比进程更小的执行单元,一个进程在执行过程中,可以产生多个线程。每个线程都有自身的产生、执行和消亡的过程。线程的状态与生命周期新建:当一个Thread类或其子类的对象被声明并创建时,新生的线程对象处于新建状态。此时它已经有了相应的内存空间和其他资源。运行:线程创建之后就具备了运行的条件,一旦轮到它来享用CPU资源时,即JVM将CPU使用权切换给该线程时,此线程的就可以脱离创建它的主线程独立开始自己的生命周期了(即run方法执行的过程)。中断:有4种原因的中断,CPU资源从当前线程切换给其他线程、执行了sleep(intmillsecond)方法、执行了wait()方法、进入阻塞状态。死亡:run方法结束。线程的创建在Java语言中,与线程支持密切相关的是java.lang.Thread类和java.lang.Runnable接口。Runnable接口定义很简单,只有一个run方法。任何一个类如果希望自己的实例能够以线程的形式执行,都可以来实现Runnable接口。继承Thread类和实现Runnable接口,都可以用来创建Thread对象,效果上并没有什么不同。继承Thread类的方法很明显的缺点就是这个类不能再继承其他的类了,而实现Runnable接口不会有这个麻烦。另外,在继承Thread类的代码中,this其实就是指当前正在运行的线程对象,如果使用实现Runnable接口的方式,要得到当前正在执行的线程,需要使用Thread.currentThread()方法。线程创建后仅仅是占有了内存资源,在JVM管理的线程中还没有这个线程,此线程必须调用start()方法(从父类继承的方法)通知JVM,这样JVM就会知道又有一个新一个线程排队等候切换了。注意:多次启动一个线程,或者启动一个已经运行的线程对象是非法的,会抛出IllegalThreadStateException异常对象。线程的优先级同一时刻在等待队列中的线程会有很多个,它们各自任务的重要性有所不同。为了加以区分,使工作安排和资源分配时间更为合理,每个线程可以被赋予不同的优先级,让任务比较急的线程拥有更高的优先级,从而更快地进入执行状态。Java中提供了10个等级的线程优先级,最低为Thread.MIN_PRIORITY=1,最高为Thread.MAX_PRIORITY=10,默认优先级为Thread.NORM_PRIORITY=5。使用Thread类中的setPriority(int)方法可以为线程指定优先级。线程的常用方法start()方法:线程调用该方法将启动线程,使之从新建状态进入就绪队列排队,一旦轮到它来享用CPU资源时,就可以脱离创建它的线程独立开始自己的生命周期了。run()方法:Thread类的run()方法与Runnable接口中的run()方法的功能和作用相同,都用来定义线程对象被调度之后所执行的操作,都是系统自动调用而用户程序不得引用的方法。系统的Thread类中,run()方法没有具体内容,所以用户程序需要创建自己的Thread类的子类,并重写run()方法来覆盖原来的run()方法。当run方法执行完毕,线程就变成死亡状态。sleep(intmillsecond)方法:现程占有CPU期间,执行sleep方法来使自己放弃CPU资源,休眠一段时间。休眠时间的长短由sleep方法的参数决定,millsecond是毫秒为单位的休眠时间。如果线程在休眠时被打断,JVM就抛出InterruptedException异常。因此,必须在try~catch语句块中调用sleep方法。isAlive()方法:线程处于“新建”状态时,线程调用isAlive()方法返回false。当一个线程调用start()方法,并占有CUP资源后,该线程的run方法就开始运行,在线程的run方法结束之前,即没有进入死亡状态之前,线程调用isAlive()方法返回true。当线程进入“死亡”状态后(实体内存被释放),线程仍可以调用方法isAlive(),这时返回的值是false。一个已经运行的线程在没有进入死亡状态时,不要再给线程分配实体,由于线程只能引用最后分配的实体,先前的实体就会成为“垃圾”,并且不会被垃圾收集机收集掉。currentThread()方法:currentThread()方法是Thread类中的类方法,可以用类名调用,该方法返回当前正在使用CPU资源的线程。interrupt()方法:intertupt方法经常用来“吵醒”休眠的线程。当一些线程调用sleep方法处于休眠状态时,一个占有CPU资源的线程可以让休眠的线程调用interrupt方法“吵醒”自己。线程的同步线程同步是指几个线程都需要调用一个同步方法(使用关键字synchronized修饰的方法)。当一个线程A使用一个synchronized修饰的方法时,其他线程想使用这个方法时就必须等待,直到线程A使用完该方法(除非线程A使用wait主动让出CPU资源)。一个线程在使用的同步方法中时,可能根据问题的需要,必须使用wait()方法使本线程等待,暂时让出CPU的使用权,并允许其它线程使用这个同步方法。其它线程如果在使用这个同步方法时如果不需要等待,那么它用完这个同步方法的同时,应当执行notifyAll()方法通知所有的由于使用这个同步方法而处于等待的线程结束等待。挂起:有时候两个线程并不是同步的,即不涉及都需要调用一个同步方法,但线程也可能需要暂时的挂起。所谓挂起一个线程就是让线程暂时让出CPU的使用权限,暂时停止执行,但停止执行的持续时间不确定,因此不能使用sleep方法暂停线程。挂起一个线程需使用wait方法,即让准备挂起的线程调用wait方法,主动让出CPU的使用权限.恢复:为了恢复该线程,其它线程在占有CUP资源期间,让挂起的线程的目标对象执行notifyAll()方法,使得挂起的线程继续执行;如果线程没有目标对象,为了恢复该线程,其它线程在占有CUP资源期间,让挂起的线程调用notifyAll()方法,使挂起的线程继续执行。实验内容1.汉字识别程序编写一个Java应用程序,在主线程中再创建一个Frame类型的窗口,在该窗口中再创建一个线程giveWord。线程giveWord每隔6秒钟给出一个汉字,用户使用一种汉字输入法将该汉字输入到文本框中。请按模板要求,将代码补充完整。WordThread.javaimportjava.awt.大;publicclassWordThreadextendsThread{charword;Labelcom;WordThread(Labelcom){=com;}publicvoidrun(){while(true){word=(char)(Math.random()*(29968-19968)+19968);System.out.println(word);com.setText(""+word);try{【补充代码】//调用sleep方法使得线程中断6000豪秒}catch(InterruptedExceptione){}}}}ThreadFrame.javapublicclassThreadFrameextendsFrameimplementsActionListener{LabelwordLabel;Buttonbutton;TextFieldinputText,scoreText;【补充代码】//用WordThread声明一个giveWord对象intscore=0;ThreadFrame(){wordLabel=newLabel("",Label.CENTER);wordLabel.setFont(newFont("",Font.BOLD,72));button=newButton("开始");inputText=newTextField(3);scoreText=newTextField(5);scoreText.setEditable(false);【补充代码】//创建giveWord,将wordLabel传递给WordThread构造方法的参button.addActionListener(this);inputText.addActionListener(this);add(button,BorderLayout.NORTH);add(wordLabel,BorderLayout.CENTER);PanelsouthP=newPanel();southP.add(newLabel(”输入标签所显示的汉字后回车:"));southP.add(inputText);southP.add(scoreText);add(southP,BorderLayout.SOUTH);setBounds(100,100,350,180);setVisible(true);validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button){if(!(【补充代码】))//giveWord调用方法isAlive(){giveWord=newWordThread(wordLabel);}try{【补充代码】//giveWord调用方法start()}catch(Exceptionexe){}}elseif(e.getSource()==inputText){if(inputText.getText().equals(wordLabel.getText())){score++;}scoreText.setText("得分:”+score);inputText.setText(null);}}}WordThread.javapublicclassThreadWordMainClass{publicstaticvoidmain(Stringargs[]){newThreadFrame();答案代码:WordThread.javaimportjava.awt.*;publicclassWordThreadextendsThread{charword;Labelcom;WordThread(Labelcom){=com;}publicvoidrun(){while(true){word=(char)(Math.random()*(29968-19968)+19968);System.out.println(word);com.setText(""+word);try{sleep(6000);//调用sleep方法使得线程中断6000豪秒}catch(InterruptedExceptione){}}}}ThreadFrame.javapublicclassThreadFrameextendsFrameimplementsActionListener{LabelwordLabel;Buttonbutton;TextFieldinputText,scoreText;WordThreadgiveWord;//用WordThread声明一个giveWord对象intscore=0;ThreadFrame(){wordLabel=newLabel("”,Label.CENTER);wordLabel.setFont(newFont("”,Font.BOLD,72));button=newButton(”开始”);inputText=newTextField(3);scoreText=newTextField(5);scoreText.setEditable(false);giveWord=newWordThread(wordLabel);//创建giveWord,将wordLabel传递给WordThread构造方法的参数button.addActionListener(this);inputText.addActionListener(this);add(button,BorderLayout.NQRTE);add(wordLabel,BorderLayout.CENTER);PanelsouthP=newPanel();southP.add(newLabel("输入标签所显示的汉字后回车:"));southP.add(inputText);southP.add(scoreText);add(southP,BorderLayout.SOUTH);setBounds(100,100,350,180);setVisible(true);validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button){if(!(giveWord.isAlive()))//giveWord调用方法isAlive(){giveWord=newWordThread(wordLabel);}try{giveWord.start();//【补充代码】//giveWord调用方法start()}catch(Exceptionexe){}}elseif(e.getSource()==inputText){if(inputText.getText().equals(wordLabel.getText())){score++;}scoreText.setText("得分:"+score);inputText.setText(null);}}}WordThread.javapublicclassThreadWordMainClass{publicstaticvoidmain(Stringargs[]){newThreadFrame();}2.双线程接力编写一个应用程序,除了主线程外,还有两个线程:first和secondofirst负责模拟一个红色的按钮从坐标(10,60)运动到(100,60);second负责模拟一个绿色的按钮从坐标(100,60)运动到(200,60)o阅读并分析以下程序,将程序中的代码补充完整,编译并运行程序,查看结果。MoveButton.javaimportjava.awt.*;importjava.awt.event.*;publicclassMoveButtonextendsFrameimplementsRunnable,ActionListener(【补充代码】〃用Thread类声明first,second两个线程对象ButtonredButton,greenButton,startButton;intdistance=10;MoveButton()(【补充代码】//创建first线程,当前窗口做为该线程的目标对象【补充代码】//创建first线程,当前窗口做为该线程的目标对象redButton=newButton();greenButton=newButton();redButton.setBackground(Color.red);greenButton.setBackground(Color.green);startButton=newButton("start");startButton.addActionListener(this);setLayout(null);add(pedButton);redButton.setBounds(10,60,15,15);add(greenButton);greenButton.setBounds(100,60,15,15);add(startButton);startButton.setBounds(10,100,30,30);setBounds(0,0,300,200);setVisible(true);validate();addWindowListener(newWindowAdapter()publicvoidwindowClosing(WindowEvente)(Systemcit(0);}});}publicvoidactionPerformed(ActionEvente)(try(first.start();second.start();}catch(Exceptionexp)(}}publicvoidrun()(while(true)(if(【补充代码】)〃判断当前占有CPU资源的线程是否是first(moveComponentredButton);try(Thread.sLeep(20);}catch(Exceptionexp)(}}if(【补充代码】)//判断当前占有CPU资源的线程是否是second(moveComponentgreenButton);try(Thread.sLeep(10);}catch(Exceptionexp)(}}}}publicsynchronizedvoidmoveComponent(Componentb)(if(Thread.currentThread()==first)(while(distance>100&&distance<=200)try(wait();}catch(Exceptionexp)(}distance=distance+1;b.setLocationdistance,60);if(distance>=100)(b.setLocation(10,60);notifyAll();}}if(Thread.currentThread()==second)(while(distance>=10&&distance<100)try(wait();}catch(Exceptionexp)(}distance=distance+1;b.setLocationdistance,60);if(distance>200)(distance=10;b.setLocation(100,60);notifyAll();}MoveButtonMainClass.javapublicclassMoveButtonMainClass(publicstaticvoidmain(Stringargs[])(newMoveButton();}}答案:MoveButton.javaimportjava.awt.*;importjava.awt.event.*;publicclassMoveButtonextendsFrameimplementsRunnable,ActionListener{/****/privatestaticfinallongseriaLVersionUID=1L;Threadfirst,second;〃用Thread类声明first,second两个线程对象ButtonredButton,greenButton,startButton;intdistance=10;MoveButton(){first=newThread(this);〃创建first线程,当前窗口做为该线程的目标对象second=newThread(this);〃创建first线程,当前窗口做为该线程的目标对象redButton=newButton();greenButton=newButton();redButton.setBackground(Color.red);greenButton.setBackground(Color.green);startButton=newButton("start");startButton.addActionListener(this);setLayout(null);add(nedButton);redButton.setBounds(10,60,15,15);add(greenButton);greenButton.setBounds(100,60,15,15);add(startButton);startButton.setBounds(10,100,30,30);setBounds(0,0,300,200);setVisible(true);validate();addWindowListener(newWindowAdapter()publicvoidwindowClosing(WindowEvente){SysteBKit(0);}});}publicvoidactionPerformed(ActionEvente){try{first.start();second.start();}catch(Exceptionexp){}}publicvoidrun(){while(true){if(Thread.currentThread()==first)〃判断当前占有CPU资源的线程是否是first{moveComponentredButton);try{Thread.sCeep(20);}catch(Exceptionexp){}}if(Thread.currentThread()==second)〃判断当前占有CPU资源的线程是否是second{moveComponentgreenButton);try{Thread.sLeep(10);}catch(Exceptionexp){}}}}publicsynchronizedvoidmoveComponent(Componentb){if(Thread.currentThread()==first){while(distance>100&&distance<=200)try{wait();}catch(Exceptionexp){}distance=distance+1;b.setLocationdistance,60);if(distance>=100){b・setLocation(10,60);notifyAll();}}if(Thread.currentThread()==second){while(distance>=10&&distance<100)try{wait();}catch(Exceptionexp){}distance=distance+1;b.setLocationdistance,60);if(distance>200){distance=10;b.setLocation(100,60);notifyAll();}}}}MoveButtonMainClass.javapublicclassMoveButtonMainClass{publicstaticvoidmain(Stringargs[]){newMoveButton();}}匹

1=1练习:在MoveButton类中再增加一个蓝色的按钮和一个匹

1=1线程,third线程负责将这个蓝色的按钮从(200,60)运动到(300,60)。MoveButton.javaimportjava.awt.*;importjava.awt.event.*;publicclassMoveButtonextendsFrameimplementsRunnable,ActionListener{/****/privatestaticfinallongserialVersionUID=1L;Threadfirst,second,third;//【补充代码】//用Thread类声明first,second两个线程对象ButtonredButton,greenButton,blueButton,startButton;intdistance=10;MoveButton(){first=newThread(this);//【补充代码】〃创建first线程,当前窗口做为该线程的目标对象second=newThread(this);//【补充代码】〃创建first线程,当前窗口做为该线程的目标对象third=newThread(this);redButton=newButton();greenButton=newButton();blueButton=newButton();redButton.setBackground(Color.red);greenButton.setBackground(Color.green);blueButton.setBackground(Color.blue);startButton=newButton("start");startButton.addActionListener(this);setLayout(null);add(pedButton);redButton.setBounds(10,60,15,15);add(greenButton);greenButton.setBounds(100,60,15,15);add(blueButton);blueButton.setBounds(200,60,15,15);add(startButton);startButton.setBounds(10,100,30,30);setBounds(0,0,350,200);setVisible(true);validate();addWindowListenernewWindowAdapter()publicvoidwindowClosing(WindowEvente){Systemdt(0);}});}publicvoidactionPerformed(ActionEvente){try{first.start();second.start();third.start();}catch(Exceptionexp)(}}publicvoidrun(){while(true){if(Thread.currentThread()==first)〃判断当前占有CPU资源的线程是否是first{moveComponentredButton);try{Thread.sleep(20);}catch(Exceptionexp){}}if(Thread.currentThread()==second)〃判断当前占有CPU资源的线程是否是second{moveComponentgreenButton);try{Thread.sleep(10);}catch(Exceptionexp){}}if(Thread.currentThread()==third){moveComponent(blueButton);try{Thread.sleep(50);}catch(Exceptionexp)(}}}publicsynchronizedvoidmoveComponent(Componentb){if(Thread.currentThread()==first){while(distance>100&&distance<=300)try{wait();}catch(Exceptionexp){}distance=distance+1;b.setLocationdistance,60);if(distance>=100){b・setLocation(10,60);notifyAll();}}if(Thread.currentThread()==second){while(distance>=10&&distance<100)try{wait();)catch(Exceptionexp)(}distance=distance+1;b.setLocationdistance,60);if(distance>200){//distance=10;b・setLocation(100,60);notifyAll();}}if(Thread.currentThread()==third){while(distance>=10&&distance<200)try{wait();}catch(Exceptionexp){}distance=distance+1;b・setLocation(distance,60);if(distance>300){distance=10;b.setLocation(200,60);notifyAll();}MoveButtonMainClass.javapublicclassMoveButtonMainClass{publicstaticvoidmain(Stringargs[]){newMoveButton();3.线程的控制编写一个程序,动画显示文本域中的字符串。1)阅读并分析以下程序,将程序中的代码补充完整,编译并运行程序,查看结果。importjava.awt.BorderLayout;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextArea;importjavax.swing.border.BevelBorder;publicclassRunnableDemoextendsJFrameimplementsRunnable(//动画显示的文本字符串privateStringintroduction="现在大家已经对计算机很熟悉了,如今计算机的操作"+”系统可以同时执行多个任务,在听歌的同时能够打字、下载文件,在聊天窗口打"+”字的时候,对方同时还能通过视频看到你;听到你。这一切都是使用多任务实现"+”的,Java语言使用多线程实现一个程序中的多个任务同时运行。程序员可以在程"+”序中执行多个线程,每一个线程完成一个功能,并与其他线程并发执行,这种机"+”制被称为多线程。”;JTextAreatextArea;//文本域组件JLabellabel;Threadthread;publicRunnableDemo()(setTitle("线程的控制");label=newJLabel("多线程简介:”);//标签组件getContentPane().add(label,BorderLayout.NORTH);//添加标签到窗体textArea=newJTextArea("\t");//初始化文本域组件textArea.setBorder(newBevelBorder(BevelBorder.LOWERED));//设置边框textArea.setLineWrap(true);//设置自动折行getContentPane().add(textArea,BorderLayout.CENTER);//添加文本域组件到文本框setBounds(100,100,383,225);//设置窗体大小位置setDefaultCloseOperation(JFrame.EXI^ON_CLOSE);setVisible(true);//显示窗体【补充代码】//创建thread线程,当前窗口做为该线程的目标对象【补充代码】//启动thread线程}//Runnable接口方法,是线程的执行方法^Overridepublicvoidrun()(String[]intros=introduction.split("");//将字符串分割为数组for(Stringch:intros)(//ForEach遍历字符串数组textArea.append(ch);//添加一个字符到文本域try(【补充代码】//线程休眠0.1秒}catch(InterruptedExceptione)(e.printStackTrace();}}}publicstaticvoidmain(Stringargs[])(newRunnableDemo();//创建本类实例对象}}importjava.awt.BorderLayout;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextArea;importjavax.swing.border.BevelBorder;//importjavax.swing.plaf.SliderUI;publicclassRunnableDemoextendsJFrameimplementsRunnable(/****/privatestaticfinallongseriaLVersionUID=1L;//动画显示的文本字符串privateStringintroduction="现在大家已经对计算机很熟悉了,如今计算机的操作”+”系统可以同时执行多个任务,在听歌的同时能够打字、下载文件,在聊天窗口打"+"字的时候,对方同时还能通过视频看到你;听到你。这一切都是使用多任务实现"+”的,Java语言使用多线程实现一个程序中的多个任务同时运行。程序员可以在程"+”序中执行多个线程,每一个线程完成一个功能,并与其他线程并发执行,这种机”+”制被称为多线程。";JTextAreatextArea;//文本域组件JLabellabel;Threadthread;publicRunnableDemo()(setTitle("线程的控制");label=newJLabel("多线程简介:");//标签组件getContentPane().add(label,BorderLayout.NORTH);//添加标签到窗体textArea=newJTextArea("\t");//初始化文本域组件textArea.setBorder(newBevelBorder(BevelBorder.LOWERED));//设置边框textArea.setLineWrap(true);//设置自动折行getContentPane().add(textArea,BorderLayout.CENTER);〃添加文本域组件到文本框setBounds(100,100,383,225);//设置窗体大小位置setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);//显示窗体Threadthread=newThread(this);//【补充代码】//创建thread线程,当前窗口做为该线程的目标对象thread.start();//【补充代码】//启动thread线程}//Runnable接口方法,是线程的执行方法^Overridepublicvoidrun()(String[]intros=introduction.split("");//将字符串分割为数组for(Stringch:intros)(//ForEach遍历字符串数组textArea.append(ch);//添加一个字符到文本域try(Thread.steep(100);//【补充代码】//线程休眠0.1秒}catch(InterruptedExceptione)(e.printStackTrace();}}}publicstaticvoidmain(Stringargs[])(newRunnableDemo();//创建本类实例对象}}2)在窗体的南面添加三个按钮,为程序添加线程控制功能,要求点击开始按钮(startBtn),线程开始启动,文字逐个显示,并且将按钮状态改变为禁用(因为线程不能重复启动)点击暂停按钮(pauseBtn),线程暂停,文字显示停止;点击恢复按钮(resumeBtn),线程恢复运行,文字继续显示。当线程执行完毕后,恢复开始按钮的状态为可用。程序运行界面如下图所示:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.border.BevelBorder;publicclassRunnableDemoextendsJFrameimplementsRunnable,ActionListener(privatestaticfinallongseriaLVersionUID=1L;//动画显示的文本字符串privateStringintroduction="现在大家已经对计算机很熟悉了,如今计算机的操作”+”系统可以同时执行多个任务,在听歌的同时能够打字、下载文件,在聊天窗口打"+"字的时候,对方同时还能通过视频看到你;听到你。这一切都是使用多任务实现"+"的,Java语言使用多线程实现一个程序中的多个任务同时运行。程序员可以在程"+”序中执行多个线程,每一个线程完成一个功能,并与其他线程并发执行,这种机"+”制被称为多线程。";JTextAreatextArea;//文本域组件JLabellabel;Threadthread;JButtonstartBtn,pauseBtn,resumeBtn;booleanallow=false;publicRunnableDemo()(setTitle("线程的控制");label=newJLabel("多线程简介:");//标签组件getContentPane().add(label,BorderLayout.NORTH);//添加标签到窗体textArea=newJTextArea("\t");//初始化文本域组件textArea.setBorder(newBevelBorder(BevelBorder.LOWERED));//设置边框textArea.setLineWrap(true);//设置自动折行textArea.setEditable(false);//设置不可被编辑getContentPane().add(textArea,BorderLayout.CENTER);〃添加文本域组件到文本框startBtn=newJButton("开始");pauseBtn=newJButton("暂停");resumeBtn=newJButton("恢复");startBtn.addActionListener(this);pauseBtn.addActionListener(this);resumeBtn.addActionListener(this);JPanelpanel=newJPanel();panel.add(startBtn);panel.add(pauseBtn);panel.add(resumeBtn);getContentPane().add(panel,BorderLayout.SOUTH);setBounds(100,100,383,225);//设置窗体大小位置setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);//显示窗体//setLocationRelativeTo(null);//在屏幕中央显示}//Runnable接口方法,是线程的执行方法^Overridepublicvoidrun()(String[]intros=introduction.split("");//将字符串分割为数组for(Stringch:intros)(//ForEach遍历字符串数组synchronized(this)(while(allow)(try(wait();}catch(InterruptedExceptione)(e.printStackTrace();}}}textArea.append(ch);//添加一个字符到文本域try(Thread.steep(100);//【补充代码】//线程休眠0.1秒}catch(InterruptedExceptione)(e.printStackTrace();}}startBtn.setEnabled(true);}publicstaticvoidmain(Stringargs[])(newRunnableDemo();//创建本类实例对象}^OverridepublicvoidactionPerformed(ActionEvente)(if(e.getSource()==startBtn){textArea.setText("\t");Threadthread=newThread(this);//【补充代码】〃创建thread线程,当前窗口做为该线程的目标对象thread.start();//【补充代码】//启动thread线程startBtn.setEnabled(false);}elseif(e.getSource()==pauseBtn){allow=true;}elseif(e.getSource()==resumeBtn){allow=false;synchronized(this){notifyAll();}}}}实验总结请书写你对本次实验有哪些实质性的收获和体会,以及对本次实验有何良好的建议?参考文献:《Java2实用教程(第三版)》耿祥义、张跃平编著,清华大学出版社。《Java2实用教程(第三版)实验指导与习题解答》耿祥义、张跃平编著,清华大学出版社。《Java开发实战宝典》,李钟尉等编著,清华大学出版社。附录一一JavaCode之多态通过一个简单的例子理解多态的概念/***人民警察*/publicinterfaceIPolice(/***抓小偷*/publicvoidcatchThief();}/***一个警察,执行抓小偷任务.*/publicclassPoliceRealimplementsIPolice(publicvoidcatchThief()(Systemout.println("抓住小偷了”);}}/***另一个警察,也执行抓小偷任务.*/publicclassPoliceHypimplementsIPolice(publicvoidcatchThief()(Systemout.println("大冷天的抓什么小偷啊,不如偷个菜.”);}}/***市民*/publicclassCitizen(privateStringmName;publicCitizen(Stringname)(mName=name;}/***市民报案*/publicvoidreport(IPolicepolice)(Systemout.println(String.format("市民%s丢失手机,向警察报案抓小偷.",mName));police.catchThief();}}案情:市民虽然向警察报了案,但你不知道能不能把小偷抓住,甚至你都不知道他们有没有去抓小偷,还有可能你在电影里看到的剧情真的发生了...事情经过可能是这样:publicclassMain(publicstaticvoidmain(String[]args)(Citizencitizen=newCitizen("张三");IPolicepolice=getPoLice();citizen.report(police);}privatestaticIPolicegetPolice()(returnnew

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论