序列化与反序列化,线程与多线程。

578 阅读2分钟

一、序列化

属性类

public class FlyPig implements Serializable {

private static String age = "269";

private String name;

private String color;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}



public String toString() {
    return "FlyPig{" +
            "name='" + name + '\'' +
            ", color='" + color + '\'' +
            ", age='" + age + '\'' +
            '}';
}

}

测试类

 public class SerializableTest {
 
public static void main(String[] args) throws Exception {

 serializeFlyPig();
    
 FlyPig flyPig = deserializeFlyPig();
    
 System.out.println(flyPig.toString());

}

private static void serializeFlyPig() throws IOException { **(序列化)**

    FlyPig flyPig = new FlyPig();
    flyPig.setColor("black");
    flyPig.setName("小猪佩奇");

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:/flyPig.txt")));
    oos.writeObject(flyPig);

    System.out.println("FlyPig 对象序列化成功!");
    oos.close();
}

private static FlyPig deserializeFlyPig() throws Exception { **(反序列化)**

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:/flyPig.txt")));
    FlyPig person = (FlyPig) ois.readObject();
    System.out.println("FlyPig 对象反序列化成功!");

    return person;
}

}

序列化与反序列化:

public class SerializeUtil {

/**
 * 序列化
 *
 * @param object
 * @return
 */
public static String serialize(Object object) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ObjectOutputStream oos = null;

    try {

        oos = new ObjectOutputStream(bos);

        oos.writeObject(object);

        oos.flush();

        return new BASE64Encoder().encode(bos.toByteArray());

    } catch (IOException e) {

        System.out.println("序列化错误");
        e.printStackTrace();

    } finally {

        try {

            if (oos != null) {

                oos.close();

            }

            bos.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    return null;

}


/**
 * 反序列化
 *
 * @param object
 * @return
 */
public static Object unserialize(String object) {

    ByteArrayInputStream bis = null;

    ObjectInputStream ois = null;

    try {

        bis = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(object));

        ois = new ObjectInputStream(bis);

        Object o = ois.readObject();

        return o;

    } catch (IOException e) {

        System.out.println("反序列化错误:IO异常");
        e.printStackTrace();

    } catch (ClassNotFoundException e) {

        System.out.println("反序列化错误:类找不到");
        e.printStackTrace();

    } finally {

        try {

            if (bis != null) {

                bis.close();
            }
            if (ois != null) {
                ois.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    return null;

}

}

二、线程与多线程

线程:线程是一个系统里面不同的执行路径

线程实现的两种方式:

一、实现runnable接口

二、继承Thread类(尽量使用接口)

有锁:synchronized

等待唤醒:this.wait与this.notify(写在try里面)

方法调用 用Runnable r = new Runnable();r.run.

线程启动 用Thread t = new Thread(r);t.start

一、

public class MyThread {
    public MyThread(String s) {

    }

    public void start() {

    }
}

二、

public class TestThread {
public static void main(String args[]) {
   MyThread3 t1 = new MyThread3("t1");/* 同时开辟了两条子线程t1和t2,t1和t2执行的都是run()方法 */
   /* 这个程序的执行过程中总共有3个线程在并行执行,分别为子线程t1和t2以及主线程 */
   MyThread3 t2 = new MyThread3("t2");
   t1.start();// 启动子线程t1
   t2.start();// 启动子线程t2
   for (int i = 0; i <= 5; i++) {
       System.out.println("I am main Thread");
   }
    }
}

class MyThread3 extends Thread {
    MyThread3(String s) {
        super(s);
    }
public void run() {
  for (int i = 1; i <= 5; i++) {
    System.out.println(getName() + ":" + i);
    if (i % 2 == 0) {
     yield();// 当执行到i能被2整除时当前执行的线程就让出来让另一个在执行run()方法的线程来优先执行
     /*
      * 在程序的运行的过程中可以看到,
      * 线程t1执行到(i%2==0)次时就会让出线程让t2线程来优先执行
      * 而线程t2执行到(i%2==0)次时也会让出线程给t1线程优先执行
      */
    }
  }
    }
}

测试:

public class ThreadEx extends Thread{

public ThreadEx(String name){
    super(name);
}
public void run(){
  System.out.println(this.getName()+"打印信息");
  try {
      Thread.sleep(1000);
  } catch (InterruptedException e) {
      e.printStackTrace();
  }
}

}