我是靠谱客的博主 优雅月饼,这篇文章主要介绍java 第八次实验:多线程与异常处理,现在分享给大家,希望可以做个参考。

一、实验目的:

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

 

二、实验环境:

    Eclipse

 

三、实验内容:

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

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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程序文件,源代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 文件,源代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
<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 程序文件,源代码如下。 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 程序文件,源代码如下。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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?"); 有没有被执行?

答:没有。


最后

以上就是优雅月饼最近收集整理的关于java 第八次实验:多线程与异常处理的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部