实验四 多线程异常处理.docx

上传人:b****8 文档编号:8963883 上传时间:2023-05-16 格式:DOCX 页数:17 大小:545.78KB
下载 相关 举报
实验四 多线程异常处理.docx_第1页
第1页 / 共17页
实验四 多线程异常处理.docx_第2页
第2页 / 共17页
实验四 多线程异常处理.docx_第3页
第3页 / 共17页
实验四 多线程异常处理.docx_第4页
第4页 / 共17页
实验四 多线程异常处理.docx_第5页
第5页 / 共17页
实验四 多线程异常处理.docx_第6页
第6页 / 共17页
实验四 多线程异常处理.docx_第7页
第7页 / 共17页
实验四 多线程异常处理.docx_第8页
第8页 / 共17页
实验四 多线程异常处理.docx_第9页
第9页 / 共17页
实验四 多线程异常处理.docx_第10页
第10页 / 共17页
实验四 多线程异常处理.docx_第11页
第11页 / 共17页
实验四 多线程异常处理.docx_第12页
第12页 / 共17页
实验四 多线程异常处理.docx_第13页
第13页 / 共17页
实验四 多线程异常处理.docx_第14页
第14页 / 共17页
实验四 多线程异常处理.docx_第15页
第15页 / 共17页
实验四 多线程异常处理.docx_第16页
第16页 / 共17页
实验四 多线程异常处理.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验四 多线程异常处理.docx

《实验四 多线程异常处理.docx》由会员分享,可在线阅读,更多相关《实验四 多线程异常处理.docx(17页珍藏版)》请在冰点文库上搜索。

实验四 多线程异常处理.docx

实验四多线程异常处理

实验四多线程异常处理

1.实验目的

(1)掌握Java的异常处理机制及相关实现方法能够在程序设计中熟练运用异常及相关类及对象;

(2)掌握Java多线程的概念和实现方法;

(3)掌握Java多线程的同步问题;

2.实验内容

实验题1创建两个线程,一个线程打印“A”,另一个线程打印“B”,比如ABBABAABBA….

代码:

packagecn.shangji.ThreadDemo;

publicclassXt1implementsRunnable{

/**

*@paramargs

*/

staticThreadthread1=null;

staticThreadthread2=null;

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

Xt1xt=newXt1();

thread1=newThread(xt);

thread2=newThread(xt);

thread1.start();

thread2.start();

}

@Override

publicvoidrun(){

//TODOAuto-generatedmethodstub

while(true){

if(Thread.currentThread()==thread1){

try{

Thread.sleep(100);

System.out.print("A");

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

if(Thread.currentThread()==thread2){

try{

Thread.sleep(100);

System.out.print("B");

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

}

}

运行结果:

实验题2假设有火车票1000张,创建10个线程模拟10个售票点,每个售票点100毫秒买一张票。

打印出售票过程,注意使用synchronized确保同一张票只能卖出一次。

输出格式如下:

第4售票点卖出第100张票

第2售票点卖出第101张票……

代码:

packagecn.shangji.ThreadDemo;

classTicketSystemimplementsRunnable{

intsize;//总售票数

//intnumber=0;//

inti=1;//售票序号

booleanavailable=true;

Threadthread[]=newThread[10];

publicTicketSystem(intsize){

this.size=size;

productTread();

}

publicvoidproductTread(){

for(inti=0;i<10;i++){

thread[i]=newThread(this);

thread[i].setName(""+(i+1));

}

}

publicsynchronizedvoidsell(){

try{

Thread.sleep(20);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

if(available){

System.out.println("第"+Thread.currentThread().getName()

+"售票点卖出第"+(++i)+"张票");

}

if(i==size){

available=false;

System.out.println("票已售完,请明天再来……");

System.exit(0);

}

}

publicvoidrun(){

//TODOAuto-generatedmethodstub

for(inti=0;i<10;i++){

if(Thread.currentThread()==thread[i]){

while(i

sell();

}

}

}

}

}

publicclassXt2{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

TicketSystemts=newTicketSystem(100);

for(inti=0;i<10;i++){

ts.thread[i].start();

}

}

}

运行结果:

实验题3假设某家银行,它可接受顾客的汇款,每做一次汇款,便可计算出汇款的总额。

现有两个顾客,每人都分3次,每次100元将钱到入。

试编写一个程序,模拟实际作业。

程序如下:

classCBank

{privatestaticintsum=0;

publicstaticvoidadd(intn){

inttmp=sum;

tmp=tmp+n;//累加汇款总额

try{

Thread.sleep((int)(10000*Math.random()));//小睡几秒钟

}

catch(InterruptedExceptione){}

sum=tmp;

System.out.println("sum="+sum);

}

}

classCCustomerextendsThread//CCustomer类,继承自Thread类

{publicvoidrun(){//run()method

for(inti=1;i<=3;i++)

CBank.add(100);//将100元分三次汇入

}

}

publicclassEx6_1

{publicstaticvoidmain(Stringargs[])

{CCustomerc1=newCCustomer();

CCustomerc2=newCCustomer();

c1.start();c2.start();

}

}

[基本要求]运行程序5次,观察每次运行结果是否相同。

经验证知每次运行结果不相同。

[思考问题]程序运行结果每次是否相同,运行时间是否相同,为什么?

运行结果每次都不相同,运行时间也不相同,因为Thread.sleep((int)(1000*Math.random()));该语句随机产生一个时间。

1要使程序运行结果每次相同,应该怎样修改程序?

在CBank类中add(intn)的方法中加入synchronized,使得线程同步。

②程序中为什么要使用异常处理?

因为Thread的方法:

publicstaticvoidsleep(longmillis,

intnanos)

throwsInterruptedException

抛出异常,所以必须对异常进行处理

实验题4在程序中主动产生一个ArithmeticException类型被0除的异常,并用catch语句捕获这个异常。

最后通过ArithmeticException类的对象e的方法getMessage给出异常的具体类型并显示出来。

代码:

packagecn.shangji.ThreadDemo;

publicclassxt4{

/**

*@paramargs

*/

privatestaticinti=12;

privatestaticintj=0;

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

try{

intsum=i/j;

}catch(ArithmeticExceptione){

//TODO:

handleexception

System.out.println("通过e的方法getMessage显示异常详细消息为:

");

System.out.println(e.getMessage());

}

}

}

运行结果:

实验题5在一个类的静态方法mathod()方法内使用throw产生异常,使用throws子句抛出mathod()的异常,在main方法中捕获处理异常。

代码:

packagecn.shangji.ThreadDemo;

publicclassxt5{

/**

*@paramargs

*@throwsException

*/

publicstaticvoidthrowException()throwsException{

try{

thrownewException("新建自定义异常");

}catch(Exceptione){

//TODO:

handleexception

throwe;

}

}

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

try{

throwException();

}catch(Exceptione){

//TODO:

handleexception

System.out.println("捕获异常"+e);

}

}

}

运行结果:

实验题6在try子句中设计两个以上可能出现的异常。

通过多个catch子句分别来捕获异常。

并使用finally子句确保一段代码不管发生什么异常都能被执行。

代码:

packagecn.shangji.ThreadDemo;

importjava.io.FileReader;

importjava.io.IOException;

publicclassXt6{

/**

*@paramargs

*/

staticinti;

staticStringstr="习题六";

staticinta[]=newint[10];

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

try{

System.out.println(a[11]);

FileReaderfsr=newFileReader("xt6.java");

}catch(ArrayIndexOutOfBoundsExceptione){

//TODO:

handleexception

System.out.println(e);

}

catch(IOExceptionioe){

str=ioe.getMessage();

System.out.println(ioe);

}

finally{

System.out.println("finally语句执行!

");

}

}

}

运行结果:

实验题7编写一个程序,输入一个班某门课程成绩,统计及格人数、不及格人数平均分。

为此设计一个异常类,当输入的成绩小于0分或大于100分时,抛出异常,程序将捕捉这个异常,并作出相应处理。

[基本要求]编写完整程序

代码:

packagecn.shangji.ThreadDemo;

importjava.util.Scanner;

@SuppressWarnings("serial")

classScoreExceptionextendsException{

Stringmessage;

publicScoreException(doublei){

message="当请输入的成绩:

"+i+"不合理,请重新输入!

";

}

publicStringtoString(){

returnmessage;

}

}

publicclassXt7{

/**

*@paramargs

*/

intpass,unPass,n;

doublesum;

publicXt7(intn){

this.n=n;

}

publicvoidsetScore()throwsScoreException{

doublei=0;

for(intj=0;j

Scannersc=newScanner(System.in);

i=sc.nextDouble();

if(i>100||i<0){

thrownewScoreException(i);

}else{

if(i>=60){

++pass;

}

else

++unPass;

}

sum+=i;

}

}

publicvoidshow(){

System.out.println("及格人数为:

"+pass);

System.out.println("不及格人数为:

"+unPass);

System.out.println("平均分为:

"+sum/n);

}

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

System.out.println("请输入要统计的人数:

");

Scannersc1=newScanner(System.in);

intm=sc1.nextInt();

Xt7score=newXt7(m);

System.out.println("请输入成绩:

");

try{

score.setScore();

score.show();

}catch(ScoreExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

运行结果:

如果出现异常:

感想:

本次实验复习了异常类,对异常处理熟悉了。

异常类是个很重要的类,很多时候都需要加入异常处理。

可以根据异常修改程序的错误。

线程可以实现多个任务同时执行,提高cpu利用率。

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2