有新增方法,不能使用多态!!!
.properties||.xml配置文件的创建及读取内容
1 //创建对象 2 Properties properties = new Properties(); 3 //存储 4 properties.setProperty("driver", "com.mysql.jdbc.Driver"); 5 properties.setProperty("url", "jdbc:mysql://localhost:3306/userdb"); 6 properties.setProperty("user", "root"); 7 properties.setProperty("password", "123456"); 8 9 //存储到绝对路径盘符 10 properties.store(new FileOutputStream(new File("e://home/soldier/SOLDIER/idea_project/Order/jdbc.properties")), "db配置"); 11 properties.storeToXML(new FileOutputStream(new File("e://home/soldier/SOLDIER/idea_project/Order/c3p0.xml")), "数据库连接池"); 12 //使用相对路径-->当前工程 13 properties.store(new FileOutputStream(new File("jdbc.properties")), "db配置"); 14 properties.storeToXML(new FileOutputStream(new File("c3p0.xml")), "数据库连接池"); 15 //使用相对路径-->当前工程下的xxx 16 properties.store(new FileOutputStream(new File("src/com/soldier/db/jdbc.properties")), "db配置"); 17 18 //读取绝对路径 19 properties.load(new FileReader("e://home/soldier/SOLDIER/idea_project/Order/jdbc.properties")); 20 //使用相对路径-->当前工程 21 properties.load(new FileReader("jdbc.properties")); 22 //使用相对路径-->当前工程下的xxx 23 properties.load(new FileReader(new File("src/com/soldier/db/jdbc.properties"))); 24 25 //如果没有就输出后面的默认值 26 System.out.println(properties.getProperty("user", "null"));
容器:Set集合与Map集合
1、Set:无序(没有下标) 集合中的元素不重复;hashset具有去重功能
2、Map:由数组和链表组成
Map的简单使用:
1 /** 2 * 统计每个单词出现的次数 3 */ 4 class Demo1 { 5 public static void main(String[] args) { 6 String str = "this is a cat and that is a mice and where is the food"; 7 //分割字符串 8 String[] strArray = str.split(" "); 9 //存储到Map中 10 Map<String, Letter> letterMap = new HashMap<String, Letter>(); 11 12 for (String temp:strArray) { 13 //1、为所有key创建容器 14 if (!letterMap.containsKey(temp)) { 15 Letter letter = new Letter(); 16 letter.setCount(1); //如果是第一次进入容器 17 letterMap.put(temp, letter); 18 } else { 19 Letter letter = letterMap.get(temp); 20 letter.setCount(letter.getCount()+1); //如果容器中已近存在,那么个数+1 21 } 22 } 23 24 //输出Map的值 25 Set<String> keys = letterMap.keySet(); //放到Set容器 26 for (String key : keys) { 27 Letter letter = letterMap.get(key); 28 System.out.println("单词:【"+key+"】出现了"+letter.getCount()+"次"); 29 } 30 } 31 } 32 33 class Letter { 34 private String name; 35 private int count; 36 37 public Letter() { 38 super(); 39 } 40 public Letter(String name, int count) { 41 this.name = name; 42 this.count = count; 43 } 44 45 public String getName() { 46 return name; 47 } 48 49 public void setName(String name) { 50 this.name = name; 51 } 52 53 public int getCount() { 54 return count; 55 } 56 57 public void setCount(int count) { 58 this.count = count; 59 } 60 }
与迭代器的结合使用: 用到知识点--Map.Entry使用详解
1 //学生 2 class Student { 3 private String name; 4 private String no; //班级编号 5 private double score; 6 7 public Student() { 8 super(); 9 } 10 11 public Student(String name, String no, double score) { 12 this.name = name; 13 this.no = no; 14 this.score = score; 15 } 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public String getNo() { 26 return no; 27 } 28 29 public void setNo(String no) { 30 this.no = no; 31 } 32 33 public double getScore() { 34 return score; 35 } 36 37 public void setScore(double score) { 38 this.score = score; 39 } 40 } 41 //班级 42 class classRoom { 43 private String no; 44 private List<Student> studentList; 45 private double total; //总分 46 47 public String getNo() { 48 return no; 49 } 50 51 public void setNo(String no) { 52 this.no = no; 53 } 54 55 public List<Student> getStudentList() { 56 return studentList; 57 } 58 59 public void setStudentList(List<Student> studentList) { 60 this.studentList = studentList; 61 } 62 63 public double getTotal() { 64 return total; 65 } 66 67 public void setTotal(double total) { 68 this.total = total; 69 } 70 71 public classRoom() { 72 studentList = new ArrayList<Student>(); 73 } 74 75 public classRoom(String no) { 76 this(); 77 this.no = no; 78 } 79 } 80 81 class Demo2 { 82 83 public static void main(String[] args) { 84 List<Student> studentList = new ArrayList<Student>(); 85 studentList.add(new Student("a","001",80)); 86 studentList.add(new Student("b","001",80)); 87 studentList.add(new Student("c","003",80)); 88 studentList.add(new Student("d","003",80)); 89 studentList.add(new Student("e","005",80)); 90 91 //统计班级总分 92 Map<String, classRoom> roomMap = new HashMap<String, classRoom>(); 93 count(roomMap, studentList); 94 95 //打印 96 printScore(roomMap); 97 } 98 public static void count(Map<String, classRoom> roomMap, List<Student> studentList) { 99 for (Student student : studentList) { 100 String no = student.getNo(); 101 //根据班级编号查看Map是否存在该班级 分拣思路 102 classRoom room = roomMap.get(no); 103 if (null == room) { //第一次 104 room = new classRoom(no); 105 roomMap.put(no, room); 106 } 107 108 //存储 109 room.setTotal(room.getTotal()+ student.getScore()); 110 room.getStudentList().add(student); //学生加入班级 111 } 112 } 113 public static void printScore(Map<String, classRoom> roomMap) { 114 //entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry。 115 Set<Map.Entry<String, classRoom>> entrySet = roomMap.entrySet(); 116 Iterator<Map.Entry<String, classRoom>> it = entrySet.iterator(); //赋值到迭代器 117 while (it.hasNext()) { //从-1开始,如果有下一位 118 Map.Entry<String, classRoom> entry = it.next(); 119 classRoom room = entry.getValue(); 120 double avg = room.getTotal()/room.getStudentList().size(); 121 System.out.println("班号为:"+room.getNo()+",总分"+room.getTotal()+",平均分"+avg); 122 } 123 124 } 125 126 }
容器:Iterator接口(迭代器)
实现:List、Map、Set均可这样子迭代遍历
容器:排序容器
1、内置类比较_Comparable与Comparator_排序工具类
2、排序容器_TreeSet与TreeMap
容器:Collections工具类
实现简单洗牌与发牌:
1 public static void main(String[] args) { 2 List<Integer> cards = new ArrayList<Integer>(); 3 for (int i = 1; i <= 54; i++) { 4 cards.add(i); 5 } 6 //洗牌 7 Collections.shuffle(cards); 8 //依次发牌 9 List<Integer> p1 = new ArrayList<Integer>(); 10 List<Integer> p2 = new ArrayList<Integer>(); 11 List<Integer> p3 = new ArrayList<Integer>(); 12 List<Integer> last = new ArrayList<Integer>(); 13 for (int i = 0; i < 51; i+=3) { 14 p1.add(cards.get(i)); 15 p2.add(cards.get(i+1)); 16 p3.add(cards.get(i+2)); 17 } 18 last.add(cards.get(51)); 19 last.add(cards.get(52)); 20 last.add(cards.get(53)); 21 22 System.out.println("第一个人:"+p1); 23 System.out.println("第二个人:"+p2); 24 System.out.println("第三个人:"+p3); 25 System.out.println("底牌:"+last); 26 27 }
常用算法:
队列:Queue(单向)、Deque(双向)
银行排队
1 Queue<Request> queue = new ArrayDeque<Request>(); 2 for (int i = 0; i < 10; i++) { 3 final int num = i; 4 queue.offer(new Request() { 5 @Override 6 public void execute() { 7 System.out.println("第"+num+"个人办理存款业务,额度为:"+ (Math.random())*10000 ); 8 } 9 }); 10 } 11 while (queue.poll() != null) { 12 queue.poll().execute(); 13 }
泛型
1、泛型类:泛型没有多态,声明时也不能使用通配符‘?’
View Code
2、泛型方法:只能使用信息,不能修改
View Code
3、泛型擦除
View Code
IO流File类
//建立联系(绝对路径)src.getPath()即可获取路径 File src = new File("e://home/soldier/SOLDIER/idea_project/x.jpg"); //相对路径 src.getPath()即可获取路径 String parentPath ="e://home/soldier/SOLDIER/idea_project"; String name ="x.jpg"; File src =new File(parentPath,name); src =new File(new File(parentPath),name); //没有盘符: 以 user.dir构建,即当前工作空间,要src.getAbsolutePath()才能获取路径 src =new File("test.txt"); src =new File(".");
常用方法:
- 文件名
getName():文件名、路径名 getPath():路径名 getAbsoluteFile():绝对路径所对应的File对象 getAbsolutePath():绝对路径名 getParent() :父目录 ,相对路径的父目录,可能为null 如. 删除本身后的结果
- 判断信息
exists():是否存在 canWrite():是否可写 canRead():是否可读 isFile():是文件 isDirectory():是文件夹 isAbsolute():消除平台差异,ie以盘符开头,其他以/开头
- 长度 字节数 不能读取文件夹的长度
length()
- 创建、删除
createNewFile():不存在创建新文件,存在返回false delete():删除文件 static createTempFile:(前缀3个字节长,后缀默认.temp) 默认临时空间 staticcreateTempFile:(前缀3个字节长,后缀默认.temp,目录) deleteOnExit():退出虚拟机删除,常用于删除临时文件
- 操作目录
mkdir():创建目录,必须确保 父目录存在,如果不存在,创建失败 mkdirs():创建目录,如果父目录链不存在一同创建 list():子文件|子目录,以名字符串形式的List集合
String[] subNames =src.list();
for(String temp:subNames){
System.out.println(temp);
} listFiles():详细输出子文件和子目录的路径
File[] subFiles =src.listFiles(); //方法1(File对象)
subFiles =src.listFiles(new FilenameFilter(){ //方法2(子文件.java对象)
//命令设计模式
@Override
//dir 代表src
public boolean accept(File dir, String name) {
//System.out.println(dir.getAbsolutePath());
return new File(dir,name).isFile()&&name.endsWith(".java");
}
});
for(File temp:subFiles){
System.out.println(temp.getAbsolutePath());
}
static listRoots():根路径
IO节点流与字节流操作一样,但只能处理纯文本,全部为可见字符(.txt 、.html)
不同之处:
- 流的选择:
Reader的子类FileReader Writer的子类FileWriter
- 读取:(不用byte必须用char数组)
char[] car =new char[1024];
-
写出:
//可以不适用数组,直接用字符或者字符串 //方法有两种 wr.write(msg); wr.append(msg);
IO字节流、节点流文件的读取
- 建立联系 File对象
File src = new File("e://home/soldier/SOLDIER/idea_project/文件的读取.text");
- 选择流 文件输入流 InputStream或者FileInputStream
InputStream is =null; //提升作用域 try { is =new FileInputStream(src);
- 操作 : byte[] car =new byte[1024]; +read+读取大小 **输出
byte[] car =new byte[1024]; int len =0; //接收 实际读取大小 //循环读取 StringBuilder sb =new StringBuilder(); while(-1!=(len=is.read(car))){ //输出 字节数组转成字符串 String info =new String(car,0,len); sb.append(info); } System.out.println(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("文件不存在"); } catch (IOException e) { e.printStackTrace(); System.out.println("读取文件失败"); }finally{ try {
- 释放资源 :关闭
if (null != is) { is.close(); } } catch (Exception e2) { System.out.println("关闭文件输入流失败"); } }
IO字节流、节点流文件的写出和追加文件
- 建立联系 File对象 目的地
File dest = new File("e://home/soldier/SOLDIER/idea_project/文件的写出.text");
- 选择流 文件输出流 OutputStream的子类FileOutputStream (如果为true为追加文件,false为覆盖文件)默认false
OutputStream os =null; //以追加形式 写出文件 必须为true 否则为覆盖 try { os =new FileOutputStream(dest,true);
- 操作 : write() +flush ××(os.write(data,0,data.length)-->指定写出的起始位置及终止位置)
String str="IO字节流、节点流文件的写出 \r\n"; //写出文件内容,字符串转字节数组 byte[] data =str.getBytes(); os.write(data,0,data.length); os.flush(); //强制刷新出去 } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("文件未找到"); } catch (IOException e) { e.printStackTrace(); System.out.println("文件写出失败"); }finally{
- 释放资源 :关闭
try { if (null != os) { os.close(); } } catch (Exception e2) { System.out.println("关闭输出流失败"); } }
IO字节流、节点流文件的拷贝(程序为桥梁(文件存在会替换覆盖))
- 建立联系 File对象 源头及目的地
File src =new File(srcPath); File dest =new File(destPath);
- 选择流
文件输入流 InputStream的子类FileInputStream
文件输出流 OutputStream的子类FileOutputStreaInputStream inPut =new FileInputStream(src); OutputStream outPut =new FileOutputStream(dest);
- 操作 : 拷贝
byte[] flush =new byte[1024]; int len =0; while(-1!=(len=输入流.read(flush))){ 输出流.write(flush,0,len) } 输出流.flush // //强制刷新出去
- 释放资源 :关闭 两个流(规则:先打开的后关闭)
outPut.close(); inPut.close();
IO字节流、节点流文件夹的拷贝(文件夹存在会报错)
- 文件 赋值 copyFile
- 文件 创建 mkdirs()
- 递归查找子孙级
1 class CopyDir { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 //源目录 8 String srcPath="E:/xp/test/a"; 9 //目标目录 10 String destPath="E:/xp/test/a/b"; 11 try { 12 FileUtil.copyDir(srcPath,destPath); 13 } catch (FileNotFoundException e) { 14 e.printStackTrace(); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 19 } 20 /** 21 * 拷贝文件夹 22 * @param src 源路径 23 * @param dest 目标路径 24 */ 25 public static void copyDir(String srcPath,String destPath){ 26 File src=new File(srcPath); 27 File dest =new File(destPath); 28 copyDir(src,dest); 29 } 30 31 32 33 /** 34 * 拷贝文件夹 35 * @param src 源File对象 36 * @param dest 目标File对象 37 */ 38 public static void copyDir(File src,File dest){ 39 if(src.isDirectory()){ //文件夹 40 dest =new File(dest,src.getName()); 41 } 42 copyDirDetail(src,dest); 43 } 44 45 /** 46 * 拷贝文件夹细节 47 * @param src 48 * @param dest 49 */ 50 public static void copyDirDetail(File src,File dest){ 51 if(src.isFile()){ //文件 52 try { 53 FileUtil.copyFile(src, dest); 54 } catch (FileNotFoundException e) { 55 e.printStackTrace(); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 }else if(src.isDirectory()){ //文件夹 60 //确保目标文件夹存在 61 dest.mkdirs(); 62 //获取下一级目录|文件 63 for(File sub:src.listFiles()){ 64 copyDirDetail(sub,new File(dest,sub.getName())); 65 } 66 } 67 } 68 69 70 71 }
线程:继承Thread类创建线程
模拟龟兔赛跑:
- 创建多线程:继承Thread,重写run()方法线程体
- 使用线程:创建子类对象 + 对象.start()
public class App { //main()方法算第一条线程 public static void main(String[] args) { //创建子类对象 Rabbit rabbit = new Rabbit(); //第二条线程 Tortoise tortoise = new Tortoise(); //第三条线程 //调用start(),执行线程2、3 rabbit.start(); //不要调用run(),如果调用了run()就相当于只有main()一条线程 tortoise.start(); //不要调用run(),如果调用了run()就相当于只有main()一条线程 //执行线程1 for (int i = 0; i < 50; i++) { System.out.println("Main--》"+i); } } } class Rabbit extends Thread { @Override public void run() { //线程体 for (int i = 0; i < 50; i++) { System.out.println("兔子跑了"+i+"步"); } } } class Tortoise extends Thread { @Override public void run() { //线程体 for (int i = 0; i < 50; i++) { System.out.println("乌龟跑了"+i+"步"); } } }
静态代理模式的简单应用
静态代理模式:(结婚)
- 真实角色
- 代理角色:持有真实角色的引用
- 二者实现相同的接口
public class StaticProxy { public static void main(String[] args) { //创建真实角色 Marry you = new You(); //创建代理角色(有新增方法,不能使用多态)+真实角色的引用 WeddingCompany company = new WeddingCompany(you); //执行任务 company.marry(); } } interface Marry { void marry(); //==public abstract void marry();自动装箱 } //真实角色 class You implements Marry { @Override public void marry() { System.out.println("你和嫦娥结婚...."); } } //代理角色 class WeddingCompany implements Marry { private Marry you; public WeddingCompany(Marry you) { this.you = you; } private void before() { //结婚前 System.out.println("布置猪窝...."); } @Override public void marry() { before(); you.marry(); after(); } private void after() { //结婚后 System.out.println("闹玉兔..."); } }
线程:实现Runnable接口(使用静态代理模式)创建线程
使用Runnable创建线程
- 类 实现 Runnable接口+重写run() -->真实角色
- 启动多线程 使用静态代理
1)、创建真实角色
2)、创建代理角色:持有真实角色的引用
3)、调用.start() 启动线程public class ProgrammerApp { public static void main(String[] args) { // 1)、创建真实角色 Programmer pro = new Programmer(); // 2)、创建代理角色:持有真实角色的引用 Thread proxy = new Thread(pro); // 3)、调用.start() 启动线程 proxy.start(); for (int i = 0; i < 50; i++) { System.out.println("一边撩妹..."); } } } class Programmer implements Runnable { @Override public void run() { for (int i = 0; i < 50; i++) { System.out.println("一边敲代码..."); } } }
线程:实现Callable_Future接口创建线程
模拟龟兔赛跑:
1 public class Call { 2 public static void main(String[] args) throws InterruptedException, ExecutionException { 3 //创建线程 4 ExecutorService ser = Executors.newFixedThreadPool(2); 5 Race tortoise = new Race("老不死", 1000L); 6 Race rabbit = new Race("小兔子", 500L); 7 //获取值 8 Future<Integer> result1 = ser.submit(tortoise); 9 Future<Integer> result2 = ser.submit(rabbit); 10 //2秒后停止线程体循环 11 Thread.sleep(2000L); 12 tortoise.setFlag(false); 13 rabbit.setFlag(false); 14 15 int num1 = (Integer)result1.get(); 16 int num2 = (Integer)result2.get(); 17 System.out.println("乌龟跑了-->" + num1 + "步"); 18 System.out.println("小兔子跑了-->" + num2 + "步"); 19 //停止服务 20 ser.shutdownNow(); 21 } 22 } 23 24 class Race implements Callable<Integer> { 25 private String name; 26 private long time; 27 private boolean flag = true; 28 private int step = 0; 29 30 public Race() { 31 } 32 33 public Race(String name) { 34 this.name = name; 35 } 36 37 public Race(String name, long time) { 38 this.name = name; 39 this.time = time; 40 } 41 42 @Override 43 public Integer call() throws Exception { 44 while(this.flag) { 45 Thread.sleep(this.time); //延时 46 ++this.step; 47 } 48 49 return this.step; 50 } 51 52 public String getName() { 53 return this.name; 54 } 55 56 public void setName(String name) { 57 this.name = name; 58 } 59 60 public long getTime() { 61 return this.time; 62 } 63 64 public void setTime(long time) { 65 this.time = time; 66 } 67 68 public boolean isFlag() { 69 return this.flag; 70 } 71 72 public void setFlag(boolean flag) { 73 this.flag = flag; 74 } 75 76 public int getStep() { 77 return this.step; 78 } 79 80 public void setStep(int step) { 81 this.step = step; 82 } 83 }
线程状态
并发:模拟抢票
public class Sleep { public static void main(String[] args) { Web12306 web = new Web12306(); new Web12306(); Thread t1 = new Thread(web, "路人甲");//线程1 Thread t2 = new Thread(web, "黄牛已");//线程2 Thread t3 = new Thread(web, "攻城师");//线程3 t1.start(); t2.start(); t3.start(); } } class Web12306 implements Runnable { private int num = 50; Web12306() { } public void run() { for(; this.num > 0; System.out.println(Thread.currentThread().getName() + "抢到了" + this.num--)) { try { Thread.sleep(500L); } catch (InterruptedException var2) { var2.printStackTrace(); } } } }
线程同步(synchronized):解决并发(多个线程访问同一份资源)-->线程安全
//主方类 public class SynDemo01 { public static void main(String[] args) { Web12306 web = new Web12306(); //一份资源 Thread t1 = new Thread(web, "路人甲"); //多个线程调用 Thread t2 = new Thread(web, "黄牛已"); Thread t3 = new Thread(web, "攻城师"); t1.start(); t2.start(); t3.start(); } } class Web12306 implements Runnable { private int num = 10; private boolean flag = true; @Override public void run() { while(this.flag) { this.test(); } } }
- 同步块:synchronized (引用类型 || this || 类.class) { }
//写在Web12306内 public void test() { if (this.num <= 0) { this.flag = false; } else { synchronized(this) {//锁定当前对象;如果这个是static方法,那么就不能用‘this’,要用类.class try { Thread.sleep(500L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "抢到了" + this.num--); } } }
- 同步方法:public synchronized void test() { }
//写在Web12306内 public synchronized void test() { if (this.num <= 0) { this.flag = false; } else { try { Thread.sleep(500L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "抢到了" + this.num--); } }
单例模式下线程
懒汉式:
class Jvm {
//声明一个私有的静态变量 private static Jvm instance = null; //构造器私有化,避免外部直接创建对象 private Jvm() { } //创建一个对外的静态方法 访问该变量,如果变量没有对象,创建对象
//double checking 双重检查,提高效率 public static Jvm getInstance(long time) { //多个线程a b c d...-->效率高 if (instance == null) { //这是为了提高效率 synchronized(Jvm.class) { //锁定Jvm类,让别人无法访问 if (instance == null) { //这是为了线程安全 try { Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } instance = new Jvm(); } } } return instance; } public static Jvm getInstance2(long time) { //正常情况下,单例不需要参数 //多个线程a b c d...-->效率不高,存在对象时也需要等待 synchronized(Jvm.class) { if (instance == null) { try { Thread.sleep(time); //延时,放大错误 } catch (InterruptedException var4) { var4.printStackTrace(); } instance = new Jvm(); } return instance; } } }
单例设计模式:确保一个类只有一个对象(懒汉式)
class JVM {
//2、声明一个私有的静态属性
private static JVM instance = null; //1、构造器私有化,避免外部直接创建对象 private JVM() { } //3、创建一个对外的公共的静态的方访问该变量,如果变量没有对象,创建该对象 public static JVM getInstance(long time) { if (null == instance) { instance = new JVM(); } return instance; } }
单例设计模式:确保一个类只有一个对象(饿汉式)
//线程绝对安全
class JVM {
//2、声明一个私有的静态属性,同事创建该对象
private static JVM instance = new JVM(); //1、构造器私有化,避免外部直接创建对象 private JVM() { } //3、创建一个对外的公共的静态的方访问该变量 public static JVM getInstance(long time) { if (null == instance) { instance = new JVM(); } return instance; } }
死锁:没有同步就没有死锁。过多的重复方法,就容易死锁.
解决方法:
1 /** 2 * 一个场景,共同资源 3 * 生产者消费者模式,信号灯发 4 * wait():等待,释放锁 sleep():不释放锁 5 * notify()/notifyAll() 唤醒 6 */ 7 public class Movie { 8 private String pic; 9 //信号灯 10 //flag-->T 生产者生产,消费者等待,生产完成后通知消费 11 //flag-->F 消费者消费,生产者等待,消费完成后通知生产 12 private boolean flag = true; 13 14 public Movie() { } 15 16 public synchronized void play(String pic) { //锁住方法 17 if (!this.flag) { 18 try { 19 this.wait(); 20 } catch (InterruptedException var4) { 21 var4.printStackTrace(); 22 } 23 } 24 25 try { 26 Thread.sleep(500L); 27 } catch (InterruptedException var3) { 28 var3.printStackTrace(); 29 } 30 31 System.out.println("生产了:" + pic); 32 this.pic = pic; 33 this.notify(); 34 this.flag = false; 35 } 36 37 public synchronized void watch() { //锁住方法 38 if (this.flag) { 39 try { 40 this.wait(); 41 } catch (InterruptedException var3) { 42 var3.printStackTrace(); 43 } 44 } 45 46 try { 47 Thread.sleep(200L); 48 } catch (InterruptedException var2) { 49 var2.printStackTrace(); 50 } 51 52 System.out.println("消费了" + this.pic); 53 this.notifyAll(); 54 this.flag = true; 55 } 56 } 57 class Player implements Runnable { 58 private Movie m; 59 60 public Player(Movie m) { 61 this.m = m; 62 } 63 @Override 64 public void run() { 65 for(int i = 0; i < 20; ++i) { 66 if (i % 2 == 0) { 67 this.m.play("左青龙"); 68 } else { 69 this.m.play("右白虎"); 70 } 71 } 72 73 } 74 } 75 class Watcher implements Runnable { 76 private Movie m; 77 78 public Watcher(Movie m) { 79 this.m = m; 80 } 81 @Override 82 public void run() { 83 for(int i = 0; i < 20; ++i) { 84 this.m.watch(); 85 } 86 87 } 88 } 89 public class App { 90 public App() { 91 } 92 93 public static void main(String[] args) { 94 Movie m = new Movie(); 95 Player p = new Player(m); 96 Watcher w = new Watcher(m); 97 (new Thread(p)).start(); 98 (new Thread(w)).start(); 99 } 100 }
任务调度: QUARTZ框架
网络编程:传输协议
××××××××××××××××××××××××××××××××××××××××
网络编程:InetAddress InetSocketAddress
public static void main(String[] args) throws UnknownHostException { //使用getLocalHost方法创建InetAddress对象 InetAddress addr = InetAddress.getLocalHost(); System.out.println(addr.getHostAddress()); //返回:192.168.1.100 System.out.println(addr.getHostName()); //输出域名||本机为计算机名 addr = InetAddress.getByName("www.163.com"); System.out.println(addr.getHostAddress()); //返回163服务器的ip:61.135.253.15 System.out.println(addr.getHostName()); //输出域名:www.163.com addr = InetAddress.getByName("61.135.253.15"); System.out.println(addr.getHostAddress()); //返回163服务器的ip:61.135.253.15 System.out.println(addr.getHostName()); //输出ip而不是域名,如果这个ip地址不存在或者域名不允许你解析则输出61.135.253.15 }
网络编程:URL爬虫原理
public static void main(String[] args) throws MalformedURLException { //绝对路径构建 URL url = new URL("http://www.baidu.com:80/index.html?uname=bjsxt"); System.out.println("协议:" + url.getProtocol()); System.out.println("域名:" + url.getHost()); System.out.println("端口:" + url.getPort()); System.out.println("资源:" + url.getFile()); System.out.println("相对路径:" + url.getPath()); System.out.println("锚点:" + url.getRef()); System.out.println("参数:" + url.getQuery()); //参数:存在锚点则返回null;存在返回正确 url = new URL("http://www.baidu.com:80/a/"); url = new URL(url, "b.txt"); System.out.println(url.toString()); }
获取资源:
public static void main(String[] args) throws IOException { URL url = new URL("http://www.baidu.com"); //主页,默认资源 BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8")); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("baidu.html"), "utf-8")); String msg = null; while((msg = br.readLine()) != null) { bw.append(msg); bw.newLine(); } bw.flush(); bw.close(); br.close(); }
网络编程:UDP编程
一、类DatagramSocket和DatagramPacket
- 客户端:
public class MyClient { public static void main(String[] args) throws IOException { //1、创建客户端+端口 DatagramSocket client = new DatagramSocket(6666); //2、准备数据 double-->字节数组 double num = 89.12;byte[];
byte[] data = convert(num); //3、打包(发送地点及端口)DatagramPacket(byte[] buf, int length, InetAddress address, int port) DatagramPacket packet = new DatagramPacket(data, data.length, new InetSocketAddress("localhost", 8888)); //4、发送数据 client.send(packet); //5、释放 client.close(); } /** * 使用字节数组作为数据源+Data输出流;有新增方法,不能使用多态 */ public static byte[] convert(double num) throws IOException { byte[] data = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeDouble(num); dos.flush(); byte[] data = bos.toByteArray(); //4、接收数据 dos.close(); return data; } } - 服务器端:
public class MyServer { public static void main(String[] args) throws IOException { //1、创建服务器端+端口 DatagramSocket server = new DatagramSocket(8888); //设置端口 //2、准备容器 byte[] container = new byte[1024]; //3、封装成包 DatagramPacket(byte[] buf, int length) DatagramPacket packet = new DatagramPacket(container, container.length); //4、接收数据 server.receive(packet); //5、分析数据 字节数组-->doublebyte[] data = packet.getData(); double data = convert(packet.getData()); System.out.println(data); //6、释放资源 server.close(); } /** * 字节数组+data输入流;这里可以使用多态,因为没有新增方法 */ public static double convert(byte[] data) throws IOException { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); double num = dis.readDouble(); dis.close(); return num; } }
网络编程:Socket通信--基于UDP编程(模拟QQ聊天室)
用到多线程的知识
- 客户端
public class Client { public static void main(String[] args) throws UnknownHostException, IOException { System.out.println("请输入名称:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String name = br.readLine(); if (!name.equals("")) { Socket client = new Socket("localhost", 9999); (new Thread(new Send(client, name))).start(); (new Thread(new Receive(client))).start(); } } }
- 服务器端
1 public class Server { 2 private List<Server.MyChannel> all = new ArrayList(); 3 4 public Server() { 5 } 6 7 public static void main(String[] args) throws IOException { 8 (new Server()).start(); 9 } 10 11 public void start() throws IOException { 12 ServerSocket server = new ServerSocket(9999); 13 14 while(true) { 15 Socket client = server.accept(); 16 Server.MyChannel channel = new Server.MyChannel(client); 17 this.all.add(channel); 18 (new Thread(channel)).start(); 19 } 20 } 21 22 private class MyChannel implements Runnable { 23 private DataInputStream dis; 24 private DataOutputStream dos; 25 private boolean isRunning = true; 26 private String name; 27 28 public MyChannel(Socket client) { 29 try { 30 this.dis = new DataInputStream(client.getInputStream()); 31 this.dos = new DataOutputStream(client.getOutputStream()); 32 this.name = this.dis.readUTF(); 33 this.send("欢迎您进入聊天室"); 34 this.sendOthers(this.name + "进入了聊天室", true); 35 } catch (IOException var4) { 36 CloseUtil.closeAll(new Closeable[]{this.dis, this.dos}); 37 this.isRunning = false; 38 } 39 40 } 41 42 private String receive() { 43 String msg = ""; 44 45 try { 46 msg = this.dis.readUTF(); 47 } catch (IOException var3) { 48 CloseUtil.closeAll(new Closeable[]{this.dis}); 49 this.isRunning = false; 50 Server.this.all.remove(this); 51 } 52 53 return msg; 54 } 55 56 private void send(String msg) { 57 if (msg != null && !msg.equals("")) { 58 try { 59 this.dos.writeUTF(msg); 60 this.dos.flush(); 61 } catch (IOException var3) { 62 CloseUtil.closeAll(new Closeable[]{this.dos}); 63 this.isRunning = false; 64 Server.this.all.remove(this); 65 } 66 67 } 68 } 69 70 private void sendOthers(String msg, boolean sys) { 71 if (msg.startsWith("@") && msg.indexOf(":") > -1) { 72 String name = msg.substring(1, msg.indexOf(":")); 73 String content = msg.substring(msg.indexOf(":") + 1); 74 Iterator var6 = Server.this.all.iterator(); 75 76 while(var6.hasNext()) { 77 Server.MyChannel otherx = (Server.MyChannel)var6.next(); 78 if (otherx.name.equals(name)) { 79 otherx.send(this.name + "对您悄悄地说:" + content); 80 } 81 } 82 } else { 83 Iterator var4 = Server.this.all.iterator(); 84 85 while(var4.hasNext()) { 86 Server.MyChannel other = (Server.MyChannel)var4.next(); 87 if (other != this) { 88 if (sys) { 89 other.send("系统信息:" + msg); 90 } else { 91 other.send(this.name + "对所有人说:" + msg); 92 } 93 } 94 } 95 } 96 97 } 98 99 public void run() { 100 while(this.isRunning) { 101 this.sendOthers(this.receive(), false); 102 } 103 104 } 105 } 106 //内部类 107 class MyChannel implements Runnable { 108 private DataInputStream dis; 109 private DataOutputStream dos; 110 private boolean isRunning; 111 private String name; 112 113 public Server$MyChannel(Server var1, Socket client) { 114 this.this$0 = var1; 115 this.isRunning = true; 116 117 try { 118 this.dis = new DataInputStream(client.getInputStream()); 119 this.dos = new DataOutputStream(client.getOutputStream()); 120 this.name = this.dis.readUTF(); 121 this.send("欢迎您进入聊天室"); 122 this.sendOthers(this.name + "进入了聊天室", true); 123 } catch (IOException var4) { 124 CloseUtil.closeAll(new Closeable[]{this.dis, this.dos}); 125 this.isRunning = false; 126 } 127 128 } 129 130 private String receive() { 131 String msg = ""; 132 133 try { 134 msg = this.dis.readUTF(); 135 } catch (IOException var3) { 136 CloseUtil.closeAll(new Closeable[]{this.dis}); 137 this.isRunning = false; 138 Server.access$0(this.this$0).remove(this); 139 } 140 141 return msg; 142 } 143 144 private void send(String msg) { 145 if (msg != null && !msg.equals("")) { 146 try { 147 this.dos.writeUTF(msg); 148 this.dos.flush(); 149 } catch (IOException var3) { 150 CloseUtil.closeAll(new Closeable[]{this.dos}); 151 this.isRunning = false; 152 Server.access$0(this.this$0).remove(this); 153 } 154 155 } 156 } 157 158 private void sendOthers(String msg, boolean sys) { 159 if (msg.startsWith("@") && msg.indexOf(":") > -1) { 160 String name = msg.substring(1, msg.indexOf(":")); 161 String content = msg.substring(msg.indexOf(":") + 1); 162 Iterator var6 = Server.access$0(this.this$0).iterator(); 163 164 while(var6.hasNext()) { 165 Server$MyChannel other = (Server$MyChannel)var6.next(); 166 if (other.name.equals(name)) { 167 other.send(this.name + "对您悄悄地说:" + content); 168 } 169 } 170 } else { 171 Iterator var4 = Server.access$0(this.this$0).iterator(); 172 173 while(var4.hasNext()) { 174 Server$MyChannel other = (Server$MyChannel)var4.next(); 175 if (other != this) { 176 if (sys) { 177 other.send("系统信息:" + msg); 178 } else { 179 other.send(this.name + "对所有人说:" + msg); 180 } 181 } 182 } 183 } 184 185 } 186 187 public void run() { 188 while(this.isRunning) { 189 this.sendOthers(this.receive(), false); 190 } 191 192 } 193 } 194 195 } 196 public class Receive implements Runnable { 197 private DataInputStream dis; 198 private boolean isRunning = true; 199 200 public Receive() { 201 } 202 203 public Receive(Socket client) { 204 try { 205 this.dis = new DataInputStream(client.getInputStream()); 206 } catch (IOException var3) { 207 var3.printStackTrace(); 208 this.isRunning = false; 209 CloseUtil.closeAll(new Closeable[]{this.dis}); 210 } 211 212 } 213 214 public String receive() { 215 String msg = ""; 216 217 try { 218 msg = this.dis.readUTF(); 219 } catch (IOException var3) { 220 var3.printStackTrace(); 221 this.isRunning = false; 222 CloseUtil.closeAll(new Closeable[]{this.dis}); 223 } 224 225 return msg; 226 } 227 228 public void run() { 229 while(this.isRunning) { 230 System.out.println(this.receive()); 231 } 232 233 } 234 } 235 public class Send implements Runnable { 236 private BufferedReader console; 237 private DataOutputStream dos; 238 private boolean isRunning; 239 private String name; 240 241 public Send() { 242 this.isRunning = true; 243 this.console = new BufferedReader(new InputStreamReader(System.in)); 244 } 245 246 public Send(Socket client, String name) { 247 this(); 248 249 try { 250 this.dos = new DataOutputStream(client.getOutputStream()); 251 this.name = name; 252 this.send(this.name); 253 } catch (IOException var4) { 254 this.isRunning = false; 255 CloseUtil.closeAll(new Closeable[]{this.dos, this.console}); 256 } 257 258 } 259 260 private String getMsgFromConsole() { 261 try { 262 return this.console.readLine(); 263 } catch (IOException var2) { 264 return ""; 265 } 266 } 267 268 public void send(String msg) { 269 try { 270 if (msg != null && !msg.equals("")) { 271 this.dos.writeUTF(msg); 272 this.dos.flush(); 273 } 274 } catch (IOException var3) { 275 this.isRunning = false; 276 CloseUtil.closeAll(new Closeable[]{this.dos, this.console}); 277 } 278 279 } 280 281 public void run() { 282 while(this.isRunning) { 283 this.send(this.getMsgFromConsole()); 284 } 285 286 } 287 }
关闭线程工具类:
public class CloseUtil { public static void closeAll(Closeable... io) { Closeable[] var4 = io; int var3 = io.length; for(int var2 = 0; var2 < var3; ++var2) { Closeable temp = var4[var2]; try { if (temp != null) { temp.close(); } } catch (Exception var6) { } } } }