java基础训练 下载本文

内容发布更新时间 : 2024/5/3 22:41:12星期一 下面是文章的全部内容请认真阅读。

一、列出指定属性

public class Demo3 { public static void main(String[] args) { //System.getProperties().list(System.out); //列出系统的全部属性 System.out.println(\系统版本为:

\+System.getProperty(\)+System.getProperty(\)+System.getProperty(\)); System.out.println(\系统给用户为:\+System.getProperty(\)); System.out.println(\当前用户目录:\+System.getProperty(\)); System.out.println(\当前用户工作目录:\+System.getProperty(\)); long startTime=System.currentTimeMillis(); int sum=0; for(int i=0;i<30000000;i++){ sum+=i; } long endTime=System.currentTimeMillis(); System.out.println(\计算所花费的时间:\+(endTime-startTime)+\毫秒。\); } }

显示结果:

系统版本为:Windows Vista6.1x86 系统给用户为:lenovo

当前用户目录:C:\\Users\\lenovo

当前用户工作目录:E:\\实验\\Java实验\\seven\\seven 计算所花费的时间:80毫秒。

二、观察对象释放

class Person{ private String name; private int age; public Person(String name,int age){ this.name=name; this.age=age; } public String toString(){ return \姓名:\+this.name+\,年龄:\+this.age; } public void finalize() throws Throwable{ System.out.println(\对象被释放——>\+this); } }

public class Demo4 { public static void main(String[] args) { Person per=new Person(\张三\,20); per=null; System.gc(); } }

显示结果:

对象被释放——>姓名:张三,年龄:20

三、编程题:

某超市门口为顾客准备了100辆手推车,每位顾客在购物时需一辆推车,在购物后把推车还回去。编写一个多线程程序,模拟超市购物。 运行结果:

超市模拟购物开始

第1001号顾客进入等车购物 第1001号顾客正在购物

第1002号顾客进入等车购物 第1001号顾客把推车还回去 第1002号顾客正在购物

第1002号顾客把推车还回去

例中假设就2辆小车,处理特殊情况,当小车不够时;如果小车多时只将sum的只修改一下即可。

class Car{ public static int sum=2; public synchronized void getCar(){ if(sum==0){ try{ super.wait(); }catch(InterruptedException e){ e.printStackTrace(); } } if(sum>0&&sum<=2){ try{ System.out.println(Thread.currentThread().getName()+\顾客正在购物\); Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } sum--; } } public synchronized void sedCar(){ if(sum>=0&&sum<2){ try{ System.out.println(Thread.currentThread().getName()+\顾客把推车还回去\); Thread.sleep(1000); }catch(InterruptedException e){

e.printStackTrace(); } sum++; super.notify(); } } }

class Buy implements Runnable{ private Car c; public Buy(Car c){ this.c=c; } public void setC(Car c){ this.c=c; } public Car getC(){ return c; } public void run(){ System.out.println(Thread.currentThread().getName()+\顾客进入等车购物\); c.getCar(); c.sedCar(); } }

public class Two { public static void main(String[] args) { System.out.println(\超市模拟购物开始\); Car c=new Car(); Buy b1=new Buy(c); Buy b2=new Buy(c); Buy b3=new Buy(c); new Thread(b1,\第1001号\).start(); new Thread(b2,\第1002号\).start(); new Thread(b3,\第1003号\).start(); } }

其中一个显示结果:

超市模拟购物开始

第1001号顾客进入等车购物 第1001号顾客正在购物 第1002号顾客进入等车购物 第1003号顾客进入等车购物 第1001号顾客把推车还回去 第1003号顾客正在购物 第1002号顾客正在购物 第1002号顾客把推车还回去 第1003号顾客把推车还回去

四、设计一个生产电脑和搬运电脑类,要求生产出一台电脑就搬走一台电脑,如果没有新的电脑生产出来,则搬运工要等待新电脑产出;如果生产出的电脑没有搬走,则要等待电脑搬走之后再生产,并统计出生产的电脑数量.

class Computer{ int num=0;

boolean flag=false; public synchronized void set(){ if(flag){ //不能生产,等待取走 try{ super.wait(); }catch(InterruptedException e){ e.printStackTrace(); } } try{ Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } num++; System.out.println(\生产了第\+num+\台电脑。\); flag=true; super.notify(); } public synchronized void get(){ if(!flag){ //不能取走,等待生产 try{ super.wait(); }catch(InterruptedException e){ e.printStackTrace(); } } try{ Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(\取走了第\+num+\台电脑。\); flag=false; super.notify(); } }

class Customer implements Runnable{ private Computer com; public void setCompu(Computer compu){ this.com=compu; } public Computer getCompu(){ return this.com; } public Customer(Computer com){ this.com=com; } public void run(){ for(int i=0;i<10;i++){ com.set(); com.get(); } } }

public class Four { public static void main(String[] args) { Computer a=new Computer(); Customer cus=new Customer(a); new Thread(cus).start(); } }

显示结果:

生产了第1台电脑。 取走了第1台电脑。 生产了第2台电脑。 取走了第2台电脑。 生产了第3台电脑。 取走了第3台电脑。 生产了第4台电脑。 取走了第4台电脑。 生产了第5台电脑。 取走了第5台电脑。 生产了第6台电脑。 取走了第6台电脑。 生产了第7台电脑。 取走了第7台电脑。 生产了第8台电脑。 取走了第8台电脑。 生产了第9台电脑。 取走了第9台电脑。 生产了第10台电脑。 取走了第10台电脑。

一、线程的休眠

class MyThread2 implements Runnable{ public void run(){ for(int i=0;i<5;i++){ try{ Thread.sleep(500); }catch(Exception e){} System.out.println(Thread.currentThread().getName()+\运行——>\+i); } } }

public class ThreadSheepDemo { public static void main(String[] args) { MyThread2 mt=new MyThread2(); Thread t=new Thread(mt,\线程\); t.start(); } }

//以上程序在执行时,每次的输出都会间隔500ms,达到了延迟操作的效果