作业一
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);
}
}