1. 企业自身分析
3.1 华为基本信息概述
第二篇:java实验报告三
西安郵電學院
《java编程及应用》课内实验报告
题 目: 异常类和多线程
院系名称: 继职学院
班 学号(8位): 3094202017
指导教师: 张 荣
设计起止时间: 20xx年5月 12日
一、设计目的
1.掌握java面向对象程序设计的基本过程,加深对java基本语法,方法的了解;
2、掌握java语言中异常类中捕获异常和定义自己的异常类
3、关于多线程:必须先了解多线程的概念,然后了解Thread类和Runnable接口。
二.设计内容
1.设计银行类中存钱和取钱时的异常处理。
2.随便选择两个城市作为预选旅游目标。实现两个线程分别显示10次城市名。每次显示后休眠一段时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口,和Thread类实现。
3·创建两个不同优先级的线程,都从1数到1000,看看哪个数得快。
三、设计与实现过程
1·设计银行类中,存钱与取钱时异常的处理机制
2·随便选择两个城市作为预选旅游目标,实现两个独立线程分别显示10次城市名,每次显示后休眠一段时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用runnable接口,和Thread类实现。
3·创建两个不同优先级的线程,都从1数到1000,看看哪个数得快。
四.测试数据及运行结果
1.
2.
3.
五.调试情况,设计技巧及体会
调试情况和心得体会
1·该设计总体上收获不小,对java有了更深的了解,实现了银行家算法的异常处理机制。和简单的多线程的编程,我觉得在编写代码方面还有待加强,在编写时应更加细心,仔细修改错误
2·在设计过程中对java语言基本语法还不够熟悉,如,继承类和接口的运用。还有一些构造方法的编写不太到位
3.在传递参数时弄得不太清楚,应当多读这方面的代码,增强编程能力。
六.附录:源代码(电子版)
1. 设计银行类中,存钱与取钱时异常的处理机制
public class SaveAndFetchMoney
{
public static void main(String[]args) { int num_of_save=20; int num_of_fetch=100; Thread[]thread=new Thread[num_of_save]; Thread[]thread1=new Thread[num_of_fetch]; Account t=new Account("007",10000); for(int i=0;i<num_of_save;i++) { thread[i]=new Thread(new thread[i].start(); SaveMoney(t,(float)Math.random()*1000)); } for(int i=0;i<num_of_fetch;i++) {
thread1[i]=new Thread(new thread1[i].start(); FetchMoney(t,(float)Math.random()*1000)); } for(int i=0;i<num_of_save;i++) { } for(int i=0;i<num_of_fetch;i++) { } try { thread1[i].join(); }catch(InterruptedException e) {e.printStackTrace();} try{ thread[i].join(); }catch(InterruptedException e) {e.printStackTrace();}
System.out.println("账号:"+t.accountName+"最终金额为:
"+t.balance);
}
class Account
{
{
System.out.print("账号:"+this.accountName+"余额为:"+this.balance); balance+=amount; String accountName; float balance; Account(String accountName, int value) { } this.accountName=accountName; this.balance=value; } public synchronized void add(float amount)
System.out.println("您存入:"+amount+"元后,现在金额为:"+this.balance); }
public synchronized void sub(float amount)
{
if (balance>=amount) {
System.out.print("账号:"+this.accountName+"余额为:
"+this.balance);
balance-=amount;
System.out.println("您取出:"+amount+"元后,现在金额为:
"+this.balance);
} else { System.out.println("账号:"+this.accountName+"余额为:
"+this.balance+"您要取出的金额为:"+amount+"余额不足!无法完成取款操作!"); }
}
}
class SaveMoney implements Runnable
{
{
Account a; float amount; FetchMoney(Account a,float amount) { } public void run() { try{ Thread.sleep((int)Math.random()*2000); this.a=a; this.amount=amount; Account a; float amount; SaveMoney(Account a,float amount) { } public void run() { } try{ Thread.sleep((int)Math.random()*2000); }catch (InterruptedException e) { e.printStackTrace(); System.out.println(e.toString()); this.a=a; this.amount=amount; } a.add(amount); class FetchMoney implements Runnable
}
} } }catch (InterruptedException e) { e.printStackTrace(); System.out.println(e.toString()); } a.sub(amount);
2·随便选择两个城市作为预选旅游目标,实现两个独立线程分别显示10次城市名,每次显示后休眠一段时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用runnable接口,和Thread类实现。
public class City extends Thread
{
}
public class City2 implements Runnable
{
private String name; public City2(String name) { this.name = name; private String name; public City(String name) { } public void run() { } int i=1; while(i<11) { } System.out.println("我最后真的下定决心决定去"+name); System.exit(0); System.out.println("我选择去"+name+"这是我第"+i+"次选择"); try { i++; Thread.sleep((int)(Math.random()*1000)); } catch (InterruptedException e) {} this.name = name;
} } public void run() { int i=1; while(i<11) { } System.out.println("我选择去"+name+"这是我的第"+i+"次选择"); try { Thread.sleep((int)(Math.random()*1000)); i++; } catch (InterruptedException e) {} System.out.println("我最后真的下决心决定去"+name); System.exit(0); }
public class demoC
{
} public static void main(String[] arg) { } choseCity thread1 = new choseCity("上海"); City2 c2 = new City2("深圳"); Thread thread2 = new Thread(c2); thread1.start(); thread2.start();
3·创建两个不同优先级的线程,都从1数到1000,看看哪个数得快。 public class hightThread extends Thread
{
public void run() { for(int i=1;i<=1000;i++) { System.out.println("高优先级线程数的数:"+i); if(i==1000) { System.out.println("高优先级线程已经把数数完了!"); }
} } }
public class demoThread
{
}
public class lowThread extends Thread {
} public void run() { } for(int i=1;i<=1000;i++) { } System.out.println("低优先级线程数的数:"+i); if(i==1000) { System.out.println("低优先级线程已经把数数完了!"); } public static void main(String[] arg) { } hightThread ht = new hightThread(); ht.setPriority(Thread.MAX_PRIORITY); lowThread t = new ht.start(); t.start(); lowThread(); t.setPriority(Thread.MIN_PRIORITY);