【Java作业】Week10——多线程
来源:互联网 发布:python 爬取搜索引擎 编辑:程序博客网 时间:2024/05/23 18:06
作业一
package org.westos.test.test01;/** * 编写一个应用程序,在线程同步的情况下来实现“生产者―消费者”问题。 */public class TestDemo01 { public static void main(String[] args) { Student01 s = new Student01(); Thread th01 = new Thread(new SetTest01(s));//生产者线程 Thread th02 = new Thread(new GetTest01(s));//消费者线程 th01.start(); th02.start(); }}package org.westos.test.test01;/** * 练习1自定义类 */public class Student01 { public String name; public int age; public boolean flag = false; public Student01() { } public Student01(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}package org.westos.test.test01;/** * 练习1 生产者 * 制造学生数据 */public class SetTest01 implements Runnable { public static int x = 0; private Student01 s; public SetTest01(Student01 s) { this.s = s; } @Override public void run() { while (true) { synchronized (s) {//同步方法 //若没有数据,就输入数据,如果有数据,就等待进入消费者线程 if (s.flag) { try { s.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } if (x % 2 == 0) { s.setName("张三"); s.setAge(23); } else { s.setName("李四"); s.setAge(24); } x++; //输入完信息之后,唤醒等待的线程,改变标记 s.flag = true; s.notify(); } } }}package org.westos.test.test01;/** * 练习1消费者 * 输出学生数据 */public class GetTest01 implements Runnable{ private Student01 s; public GetTest01(Student01 s) { this.s = s; } @Override public void run() { while (true) { synchronized (s) { if (!s.flag) {//如果没有数据,消费者线程就进入等待 try { s.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(s.getName() + "-----" + s.getAge()); s.flag = false; s.notify(); } } } }
作业二
package org.westos.test.Test02;import java.io.*;/** * 将若干个Student对象;若干个Teacher对象,写出到d:/0404/a.txt中 * 思路:通过序列化与反序列化将对象输出到txt中 * 应该吧对象写入集合中 */public class TestDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { //先建立若干个Teacher对象,和若干个Student对象 Student stu1 = new Student("张三", 13, "001"); Student stu2 = new Student("李四", 14, "002"); Student stu3 = new Student("王五", 15, "003"); Teacher t1 = new Teacher("赵六", 26, "01"); Teacher t2 = new Teacher("钱七", 27, "02"); new File("d:\\0404").mkdir(); File file = new File("d:\\0404\\a.txt"); file.createNewFile(); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(stu1); oos.writeObject(stu2); oos.writeObject(stu3); oos.writeObject(t1); oos.writeObject(t2); oos.close(); }}
作业三
File file = new File("d:\\0404\\a.txt"); //使用ObjectInputStream来阅读 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); List<Student> listS = new ArrayList<Student>(); List<Teacher> listT = new ArrayList<Teacher>(); Object obj = null; while ((obj = ois.readObject()) != null) { if (obj instanceof Student) { listS.add((Student) obj); } else { listT.add((Teacher) obj); } } ois.close();
作业四
package org.westos.test.test04;import java.util.Arrays;/** * 实现字符串和字节数组之间的相互转换,比如:将字符串”西部开源技术中心xbkyjszx”转换为字节数组,并将字节数组再转换回字符串! */public class TestDemo { public static byte[] stringToByteArray(String s) { return s.getBytes(); } public static String byteArrayToString(byte bys[]) { return new String(bys); } public static void main(String[] args) { String s = "西部开源技术中心xbkyjszx"; byte[] bys = stringToByteArray(s); System.out.print("stringToByteArray: "); for (byte b : bys) { System.out.print(b + " "); } System.out.println(); String s1 = byteArrayToString(bys); System.out.println("byteArrayToString: " + s1); }}
作业五
package org.westos.test.test05;import java.io.File;/** * 递归实现输入任意目录,列出文件以及文件夹 */public class TestDemo { private static File file = new File("src"); /** * 思路: * 通过listFiles方法求出所有子文件 * 遍历file数组 * 输出制表符来表示层级关系 * 输出遍历得到的file对象名称 * 如果是目录则递归调用 * 如果是文件则没有任何操作 * @param file 表示要输出的目录 * @param count 表示输出的制表符数量 */ public static void printFile(File file,int count) { File[] files = file.listFiles(); if (file.isDirectory()) { for(File f : files) { //增强for循环遍历file数组 //输出制表符,来形象的表示文件之间的层级关系 for (int i = 0; i < count; i++) { System.out.print("\t"); } count ++; System.out.println(f.getName());//由于文件和目录都要输出名称,所以将其抽取出来 if(f.isDirectory()) { printFile(f, count); }//如果表示得是文件,则不作任何操作 } } } public static void main(String[] args) { System.out.println(file.getName()); printFile(file,1); }}
阅读全文
0 0
- 【Java作业】Week10——多线程
- 【Java作业】Week10
- C程序设计week10作业
- week10--作业第二题~
- week10
- 作业day12——多线程
- java多线程作业
- java web week_five 多线程作业
- 第十五周作业:Java多线程
- Java复习——作业
- Java作业-猜数字(多线程)
- 第四周作业————多线程编程
- 第15周—多线程(作业1)
- 多线程作业
- 多线程作业
- 多线程作业
- 多线程作业
- 多线程作业
- [大数据入门-jQuery]Day17-jQuery对文档的操作
- maven仓库详解
- 了解java .lang 包,java.util包等常用包
- 【算法分析与设计】【第十四周】738. Monotone Increasing Digits
- GIT 删除指定COMMIT提交
- 【Java作业】Week10——多线程
- 安卓定位开发实例
- NOIP2017 游记 Day2
- 常良项目opencv调试
- js实现对上传图片进行压缩并且预览
- HDU2057 A + B Again【水题】
- 用函数判断多组数据是否是水仙花数
- HDU 1533Going Home(KM算法求二分图最小权匹配或者最小费用最大流)
- jzoj 4883. 【NOIP2016提高A组集训第12场11.10】灵知的太阳信仰