1. 程式人生 > >序列化與反序列化,執行緒與多執行緒。

序列化與反序列化,執行緒與多執行緒。

一、序列化

屬性類

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;

}

}

二、執行緒與多執行緒

執行緒:執行緒是一個系統裡面不同的執行路徑

執行緒實現的兩種方式:

1、實現runnable介面

2、繼承Thread類(儘量使用介面)

有鎖:synchronized

等待喚醒:this.wait與this.notify(寫在try裡面)

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

執行緒啟動 用Thread t = new Thread(r);t.start

1、

public class MyThread {
    public MyThread(String s) {

    }

    public void start() {

    }
}

2、

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();
  }
}

}