java 第八次实验:多线程与异常处理

一、实验目的:

    了解线程的概念、线程的生命周期,掌握多线程的编程。掌握异常的概念以及如何定义、抛出和捕捉处理异常。

 

二、实验环境:

    Eclipse

 

三、实验内容:

(一)Thread子类的方法实现多线程

编写 TwoThreadsTest.java 程序文件,源代码如下:

class SimpleThread extends Thread {
     public SimpleThread(String str) {
         super(str);
     }
     public void run() {
         for (int i = 0; i < 10; i++) {
             System.out.println(i + " " + getName());
             try {
                 sleep((int)(Math.random() * 1000));
             } catch (InterruptedException e) {}
         }
         System.out.println("DONE! " + getName());
     }
 }
public class TwoThreadsTest {
     public static void main (String[] args) {
         new SimpleThread("Go to Beijing??").start();
         new SimpleThread("Stay here!!").start();
      }
     }

运行结果:

0 Go to Beijing??
0 Stay here!!
1 Go to Beijing??
1 Stay here!!
2 Go to Beijing??
2 Stay here!!
3 Stay here!!
4 Stay here!!
3 Go to Beijing??
4 Go to Beijing??
5 Go to Beijing??
5 Stay here!!
6 Go to Beijing??
6 Stay here!!
7 Stay here!!
8 Stay here!!
7 Go to Beijing??
8 Go to Beijing??
9 Go to Beijing??
9 Stay here!!
DONE! Go to Beijing??
DONE! Stay here!!


(二)实现Runnable接口的方法实现多线程

1.程序功能:一个时钟Applet,它显示当前时间并逐秒进行更新

2.编写Clock.java程序文件,源代码如下:

import java.awt.*;
 import java.applet.*; 
 import java.util.*;
 public class Clock extends Applet implements Runnable{
        Thread clockThread;
        public void start(){
               if(clockThread==null){
                      clockThread=new Thread(this,"Clock");
                      clockThread.start();
               }
        }
        public void run(){
               while(clockThread !=null){
                      repaint();
                      try{
                             clockThread.sleep(1000);
                      }catch(InterruptedException e){}
                   }
        }
        public void paint(Graphics g){
                 Date now=new Date(); 		g.drawString(now.getHours()+";"+now.getMinutes()+";"+now.getSeconds(),5,10);
        }
        public void stop(){
               clockThread.stop();
               clockThread=null;
        }
     }

3.编译Clock.java 文件。


4.编写Clock.html 文件,源代码如下:

<HTML>
	<HEAD>
		<TITLE> A Simple Program </TITLE>
	</HEAD>
	<BODY>
		Here is the output of my program:
		<APPLET CODE="Clock.class" WIDTH=150 HEIGHT=25>
		</APPLET> 
	</BODY>
</HTML>

5.在浏览器中打开Clock.html 文件,将你看到的运行结果写在实验报告中。


(三)捕获并处理各种类型的异常

1.编写ExceptionTest.java 程序文件,源代码如下。 

package Myjava;
 
import java.io.*;
public class ExceptionTest{
       public static void main(String args[]) {
              for(int i = 0; i < 4;i++) {
                     int k;
                     try {
                           switch( i ) {
                                   case 0:        
                                          int zero = 0;
                                          k = 911 / zero;
                                          break;
                                   case 1:       
                                          int b[ ] = null;
                                          k = b[0];
                                          break;
                                   case 2:       
                                          int c[ ] = new int[2];
                                          k = c[9];
                                          break;
                                   case 3:       
                                          char ch = "abc".charAt(99);
                                          break;
                            }
                     }catch(Exception e) {
                            System.out.println("\nTestcase #" + i + "\n");
                            System.out.println(e);
                     }
              }
       }
}

2. 编译、运行

Testcase#0

 

java.lang.ArithmeticException: / by zero

 

Testcase#1

 

java.lang.NullPointerException

 

Testcase#2

 

java.lang.ArrayIndexOutOfBoundsException: 9

 

Testcase#3

 

java.lang.StringIndexOutOfBoundsException: String index out of range: 99


3.根据运行结果,请在实验报告中说明程序运行中总共捕捉了几个异常,并指出每个异常各自属于哪种类型的异常?

    4个异常。

java.lang.ArithmeticException

java.lang.NullPointerException

java.lang.ArrayIndexOutOfBoundsException

java.lang.StringIndexOutOfBoundsException


(四)了解异常处理模块中各个语句块的功能

编写TryTest.java 程序文件,源代码如下。

package Myjava;
 
import java.io.*; 
public class TryTest{
       public TryTest(){                        
              try{
                     int a[] = new int[2];
                     a[4] = 3;
                     System.out.println("After handling exception return here?");
              }catch(IndexOutOfBoundsException e){ 
                     System.err.println("exception msg:" + e.getMessage());
                     System.err.println("exception string:" + e.toString());
                     e.printStackTrace();
              }finally{
                     System.out.println("-------------------"); 
                     System.out.println("finally"); 
              }
              System.out.println("No exception?"); 
       }
       public static void main(String args[]){
              new TryTest();
       }
}

运行结果:

exceptionmsg:4

exceptionstring:java.lang.ArrayIndexOutOfBoundsException: 4

java.lang.ArrayIndexOutOfBoundsException: 4

    at Myjava.TryTest.<init>(TryTest.java:8)

    at Myjava.TryTest.main(TryTest.java:21)

-------------------

finally

No exception?


请在实验报告中指出程序中的语句System.out.println("Afterhandling exception return here?"); 有没有被执行?

答:没有。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值