作业解析
-
利用白富美接口案例,土豪征婚使用匿名内部类对象实现。
1 interface White{ 2 public void white(); 3 } 4 5 interface Rich{ 6 public void rich(); 7 } 8 9 interface Beauty{ 10 public void beauty(); 11 } 12 13 interface WRB extends White, Rich, Beauty{ 14 } 15 16 class TuHao{ 17 public void getMarry(WRB wrb){ 18 wrb.white(); 19 wrb.rich(); 20 wrb.beauty(); 21 } 22 } 23 24 class Demo{ 25 public static void main(String[] args){ 26 TuHao wsc = new TuHao(); 27 wsc.getMarry(new WRB(){ 28 public void white(){ 29 System.out.println("white"); 30 } 31 public void rich(){ 32 System.out.println("rich"); 33 } 34 public void beauty(){ 35 System.out.println("beauty"); 36 } 37 }); 38 } 39 } 40 -
定义三角形类Trianle,里面包含三个int类型属性,分别表示三条边的长度, 构造三角形对象时,任意两边之和是否大于第三边,如若不成立,抛出自定义异常。
1 class Triangle{ 2 private int a; 3 private int b; 4 private int c; 5 public Triangle(int a, int b, int c){ 6 try{ 7 if(a>=b+c || b>=a+c || c>=a+b){ 8 throw new TriangleLengthException("Invalid Length"); 9 } 10 else{ 11 this.a = a; 12 this.b = b; 13 this.c = c; 14 } 15 } 16 catch(TriangleLengthException e){ 17 e.printStackTrace(); 18 } 19 } 20 } 21 22 class TriangleLengthException extends Exception{ 23 String exceptionInfo; 24 public TriangleLengthException(String info){ 25 exceptionInfo = info; 26 } 27 28 public void printStackTrace(){ 29 System.out.println(exceptionInfo); 30 } 31 } 32 33 class TriExceptionDemo{ 34 public static void main(String[] args){ 35 Triangle t1 = new Triangle(2,3,4); 36 Triangle t2 = new Triangle(2,2,4); 37 Triangle t3 = new Triangle(2,7,4); 38 } 39 } 40 -
Person类中增加birthday属性,对setBirthday(int ,int , int )方法进行异常处理, 要求年有效、月有效、日有效、年月日指定的具体日期有效,对不同情况分别抛出不同的异常。 year:>1970, month:1-12, day:1-31
1 class Person{ 2 private int year; 3 private int month; 4 private int day; 5 public void setBirthday(int year, int month, int day){ 6 try{ 7 if(year<1970){ 8 throw new YearException("Invalid Year"); 9 } 10 else{ 11 if(month<1 || month > 12){ 12 throw new MonthException("Invalid Month"); 13 } 14 else{ 15 if(day<1 || day>31){ 16 throw new DayException("Invalid Day"); 17 } 18 else{ 19 switch(month){ 20 case 1: 21 case 3: 22 case 5: 23 case 7: 24 case 8: 25 case 10: 26 case 12: 27 break; 28 case 2: 29 if((year%400==0) || (year%4==0 && year%100!=0)){ 30 if(day>29){ 31 throw new DateException("Invalid Date"); 32 } 33 } 34 else{ 35 if(day>28){ 36 throw new DateException("Invalid Date"); 37 } 38 } 39 case 4: 40 case 6: 41 case 9: 42 case 11: 43 if(day==31){ 44 throw new DateException("Invalid Date"); 45 } 46 } 47 } 48 } 49 } 50 } 51 catch(Exception e){ 52 e.printStackTrace(); 53 } 54 } 55 } 56 57 class YearException extends Exception{ 58 String exceptionInfo; 59 public YearException(String info){ 60 exceptionInfo = info; 61 } 62 public void printStackTrace(){ 63 System.out.println(exceptionInfo); 64 } 65 } 66 67 class MonthException extends Exception{ 68 String exceptionInfo; 69 public MonthException(String info){ 70 exceptionInfo = info; 71 } 72 public void printStackTrace(){ 73 System.out.println(exceptionInfo); 74 } 75 } 76 77 class DayException extends Exception{ 78 String exceptionInfo; 79 public DayException(String info){ 80 exceptionInfo = info; 81 } 82 public void printStackTrace(){ 83 System.out.println(exceptionInfo); 84 } 85 } 86 87 class DateException extends Exception{ 88 String exceptionInfo; 89 public DateException(String info){ 90 exceptionInfo = info; 91 } 92 public void printStackTrace(){ 93 System.out.println(exceptionInfo); 94 } 95 } 96 97 class BirthdayExceptionDemo{ 98 public static void main(String[] args){ 99 Person p1 = new Person(); 100 p1.setBirthday(12,1,22); 101 p1.setBirthday(1970,13,22); 102 p1.setBirthday(2000,1,32); 103 p1.setBirthday(2000,2,28); 104 p1.setBirthday(2000,2,29); 105 p1.setBirthday(2016,2,30); 106 p1.setBirthday(2100,2,29); 107 } 108 } 109 -
将类定义到指定的包下。com.xkzhai.jar,编译之后,打成jar文件。
-
编写源文件jarDemo.java
1 package com.xkzhai.jar; 2 class Person{ 3 private String name; 4 private String sex; 5 public void run(){ 6 System.out.println("run"); 7 } 8 } 9 10 class jarDemo{ 11 public static void main(String[] args){ 12 Person p1 = new Person(); 13 p1.run(); 14 } 15 } 16 -
编译到指定的位置
1 javac -d classes2 jarDemo.java 2 -
归档
1 jar cvf test.jar -C classes2/ . 2 jar cvfe test2.jar com.xkzhai.jar.jarDemo -C classes2/ . //在清单文件中增加入口点 3 -
运行程序
1 java -cp classes2 com.xkzhai.jar.jarDemo 2 java -cp test.jar com.xkzhai.jar.jarDemo 3 java -jar test2.jar//定义好入口点的jar包 4
-
-
相互之间使用jar包,放置cp下,对class进行重用
-
编写第一个java程序Person.java
1 package com.xkzhai.jar1; 2 //不同包下的类重用,需定义为public 3 public class Person{ 4 private String name; 5 private String sex; 6 private int age; 7 public void setAge(int age){ 8 this.age = age; 9 } 10 public int getAge(){ 11 return age; 12 } 13 } 14 -
编译上述文件,放置到classes3文件夹下
1 javac -d classes3 Person.java 2 -
将类文件打包归档为jar1.jar,放置在lib文件夹下
1 jar cvf jar1.jar -C classes3/ . 2 -
重用Person类,编写第二个java程序jarDemo2.java,放置在src文件夹下
1 package cn.xkzhai; 2 import com.xkzhai.jar1.Person; 3 class Student extends Person{ 4 private String ID; 5 } 6 7 class jarDemo2{ 8 public static void main(String[] args){ 9 Student s1 = new Student(); 10 s1.setAge(20); 11 System.out.println(s1.getAge()); 12 } 13 } 14 -
编译jarDemo2.java,生成的类文件放置在classes中
1 javac -cp lib/jar1.jar -d classes src/jarDemo2.java 2 -
执行
1 java -cp lib/jar1.jar;classes cn.xkzhai.jarDemo2 2
-
-
设计程序,考查修饰符。public -> protected -> default -> private(选做题)
-
编写ArrayTool,把冒泡排序,选择排序,二分法查找等打成jar包。
-
编写java源文件ArrayTool3.java
1 package com.xkzhai.array; 2 3 class ArrayTool3{ 4 int[] arrayData; 5 6 public ArrayTool3(){ 7 } 8 9 public ArrayTool3(int[] arrayData){ 10 this.arrayData = arrayData; 11 } 12 13 //1. 打印数组元素 14 public void outArray(){ 15 for(int i: arrayData){ 16 System.out.println(i); 17 } 18 } 19 20 //2. 冒泡排序 21 // 5 4 3 2 1 22 // 4 3 2 1 5 23 // 3 2 1 4 5 24 // 2 1 3 4 5 25 // 1 2 3 4 5 26 public void bubbleSort(){ 27 for(int i = arrayData.length-1;i>0;i--){ 28 for(int j=0;j<i;j++){ 29 if(arrayData[j]>=arrayData[j+1]){ 30 int tmp = arrayData[j]; 31 arrayData[j] = arrayData[j+1]; 32 arrayData[j+1] = tmp; 33 } 34 } 35 } 36 } 37 38 //3. 选择排序 39 //5 4 3 2 1 40 //1 5 4 3 2 41 //1 2 5 4 3 42 //1 2 3 5 4 43 //1 2 3 4 5 44 public void selectSort(){ 45 for(int i=0;i<arrayData.length-1;i++){ 46 for(int j=i;j<=arrayData.length-1;j++){ 47 if(arrayData[j]<=arrayData[i]){ 48 int tmp = arrayData[j]; 49 arrayData[j] = arrayData[i]; 50 arrayData[i] = tmp; 51 } 52 } 53 } 54 } 55 56 //4. 选择排序 57 public int halfFind(int c){ 58 int min = 0; 59 int max = arrayData.length-1; 60 int index = 0; 61 62 while(min<=max){ 63 index = (min+max)/2; 64 if(arrayData[index]>c){ 65 max = index-1; 66 } 67 else if(arrayData[index]<c){ 68 min = index+1; 69 } 70 else{ 71 return index; 72 } 73 } 74 return -1; 75 } 76 } 77 78 class ArrayDemo{ 79 public static void main(String[] args){ 80 ArrayTool3 arr = new ArrayTool3(new int[]{1,3,2,20,12}); 81 arr.outArray(); 82 arr.bubbleSort(); 83 arr.outArray(); 84 85 ArrayTool3 arr2 = new ArrayTool3(new int[]{1,3,2,20,12,23,21,19}); 86 arr2.outArray(); 87 arr2.selectSort(); 88 arr2.outArray(); 89 System.out.println(arr2.halfFind(-1)); 90 System.out.println(arr2.halfFind(12)); 91 } 92 } 93 -
编译,打包
1 javac -d classes4 ArrayTool3.java 2 //java -cp classes4 com.xkzhai.array.ArrayDemo 3 jar cvf ArrayTool.jar -C classes4/ . 4
-
-
预习多线程。
进程
-
运行时(runtime)的应用程序
-
进程之间的内存不是共享(独占)
-
进程间通信使用的socket(套接字) 进程之间内存是隔离的。内存不共享。
多线程
-
程序(进程)执行过程中,并发执行的代码段
-
线程之间共享内存
-
创建灵活响应的桌面程序
-
每个运行着的线程对应一个stack(方法栈)
-
应用程序至少有一个线程(主线程)
java.lang.Thread
-
Thread.yield()方法:让当前线程让出CPU抢占权,具有谦逊之意,瞬时的动作
-
Thread.sleep(int mils)方法:让当前线程休眠指定毫秒数,放弃cpu抢占权,和锁旗标(监控权)没有关系
-
Thread.join():当前线程等待指定的线程结束后才能继续运行
-
daemon:守护线程,服务员 Thread.setDaemon(true),为其他线程提供服务的线程。若进程中剩余的线程都是守护线程的话,则进程终止了。
-
$--$ 原子性操作
-
线程间通信,共享资源的问题。锁, 将并行转串行,防止并发访问。参照物,锁旗标
1 //同步代码块 2 synchronized(object lock){ 3 ... 4 }同步代码块执行期间,线程始终持有对象的监控权,其他线程处于阻塞状态,只能等待
-
同步代码块是以当前所在对象做锁旗标 synchronized(this) === 同步方法
-
同步静态方法,使用类作为同步标记 public static synchronized xxxx(...){ }
-
wait() 让当前线程进入到锁旗标的等待队列,释放cpu抢占权,还释放锁旗标的监控权。 wait(1000); 设定等待时间,可以避免死锁
-
notify() 唤醒在等待队列中的线程,一次只通知一个线程
-
notifyAll() 通知所有线程可以抢占cpu和锁旗标监控权,解决死锁问题
class ThreadDemo10{ public static void main(String[] args){
1 Pool pool = new Pool(); 2 3 Productor p1 = new Productor("生产者1",pool); 4 p1.setName("p1"); 5 //Productor p2 = new Productor("生产者2",pool); 6 Consumer c1 = new Consumer("消费者1",pool); 7 c1.setName("c1"); 8 Consumer c2 = new Consumer("消费者2",pool); 9 c2.setName("c2"); 10 11 p1.start(); 12 //p2.start(); 13 c1.start(); 14 c2.start(); 15 }}
//生产者 class Productor extends Thread{ private String name; private Pool pool; static int i = 0;
1 public Productor(String name, Pool pool){ 2 this.name = name; 3 this.pool = pool; 4 } 5 6 public void run(){ 7 //int i=0; 8 while(true){ 9 pool.add(i++); 10 } 11 }}
//消费者 class Consumer extends Thread{ private String name; private Pool pool; public Consumer(String name, Pool pool){ this.name = name; this.pool = pool; }
1 public void run(){ 2 while(true){ 3 pool.remove(); 4 } 5 }}
//票池 class Pool{ private java.util.List<Integer> list = new java.util.ArrayList<Integer>(); //容器最大值 private int MAX = 1;
1 //添加元素 2 public void add(int n){ 3 4 synchronized(this){ 5 try{ 6 String name = Thread.currentThread().getName(); 7 while(list.size() == MAX){ 8 System.out.println(name+".wait()"); 9 this.wait(); 10 } 11 list.add(n); 12 System.out.println(name+"+: "+n); 13 System.out.println(name+".notify()"); 14 this.notifyAll(); 15 } 16 catch(Exception e){ 17 e.printStackTrace(); 18 } 19 } 20 } 21 22 //删除元素 23 public int remove(){ 24 25 synchronized(this){ 26 try{ 27 String name = Thread.currentThread().getName(); 28 while(list.size() == 0){ 29 System.out.println(name+".wait()"); 30 this.wait(); 31 } 32 int i = list.remove(0); 33 System.out.println(name+"-:"+i); 34 System.out.println(name+".notify()"); 35 this.notifyAll(); 36 return i; 37 } 38 catch(Exception e){ 39 e.printStackTrace(); 40 } 41 return -1; 42 } 43 }}
作业
-
一共100个馒头,40个工人,每个工人最多能吃3个馒头,使用多线程输出所有工人吃馒头的情况
-
5辆汽车过隧道,隧道一次只能通过一辆汽车。每辆汽车通过时间不固定, 机动车通过时间3秒,三轮车通过时间5秒,畜力车通过时间10秒,5辆车分别是2辆机动车,2辆畜力车,1辆三轮车,通过多线程模拟通过隧道的情况。提示:Car ThreeCar CowCar
-
用多线程模拟蜜蜂和熊的关系 蜜蜂是生产者,熊是消费者,蜜蜂生产蜂蜜是累加的过程,熊吃蜂蜜是批量(满20吃掉)的过程,生产者和消费者之间使用通知方式告知对方,注意不能出现死锁现象。 100只蜜蜂,每次生产的蜂蜜是1 熊吃蜂蜜是20(批量的情况)