文章出處

一、序列化與反序列化

      把對象轉換為字節序列的過程稱為對象的序列化
  把字節序列恢復為對象的過程稱為對象的反序列化
  對象的序列化主要有兩種用途:
  1) 把對象的字節序列永久地保存到硬盤上,通常存放在一個文件中;
  2) 在網絡上傳送對象的字節序列。

序列化關鍵代碼如下:

定義Person類實現序列化。重寫toString方法,定義無參以及帶參構造

定義類

public class MySerialize {

    public static void main(String[] args) throws IOException {
        OutputStream os=new FileOutputStream("save.bin");
        ObjectOutputStream oos=new ObjectOutputStream(os);
        
        List<Person> list=new ArrayList<Person>();
        Person p1=new Person("zs",12,"bj");
        Person p2=new Person("hh",22,"ah");
        Person p3=new Person("xixi",12,"hf");
        
        list.add(p1);
        list.add(p2);
        list.add(p3); 
        
        oos.writeObject(list);
        
        System.out.println("序列化成功!!!");
    }
}

反序列化關鍵代碼如下:

public class FSerialize {
 public static void main(String[] args) throws Exception {
     InputStream is=new FileInputStream("save.bin");
     ObjectInputStream ois=new ObjectInputStream(is);
    
     List<Person> list=(List<Person>)ois.readObject();
     for (Person person : list) {
         
        System.out.println(person);
    }
    
}
 
}

運行效果:

二、多線程兩種實現方式

①繼承Thread

②實現Runnable

1、繼承Thread類的方法盡管被我列為一種多線程實現方式,但Thread本質上也是實現了Runnable接口的一個實例,它代表一個線程的實例,并且,啟動線程的唯一方法就是通過Thread類的start()實例方法。start()方法是一個native方法,它將啟動一個新線程,并執行run()方法。這種方式實現多線程很簡單,通過自己的類直接extend Thread,并復寫run()方法,就可以啟動新線程并執行自己定義的run()方法。例如:

package cn.b.happy;

public class MyThread extends Thread{
@Override
public void run() {
    System.out.println("我是新線程!");
}
}
MyThread t1=new MyThread();
        System.out.println(Thread.currentThread().getName());
        t1.start();

2、如果自己的類已經extends另一個類,就無法直接extends Thread,此時,必須實現一個Runnable接口,如下:

package cn.b.happy;

public class ImplThread implements Runnable{

    @Override
    public void run() {
        
        Thread.currentThread().setName("子線程2");
        System.out.println("子線程");
        System.out.println("我是子線程");
        
        
    }

}
 ImplThread t2=new ImplThread();
     //為了啟動ImplThread,需要首先實例化一個Thread,并傳入自己的t2實例:
      Thread tt=new Thread(t2);
        tt.run();
        System.out.println(Thread.currentThread().getName());
    }

三、

join():暫停某個線程

setDaemon()后臺線程,又稱守護線程,兩個線程交替執行,當一個線程結束時,另一個線程也結束

Sleep():使線程休眠,單位是毫秒

關鍵代碼:

package cn.c.happy;

public class SleepThread extends Thread{
    @Override
    public void run() {
        Thread.currentThread().setName("子線程");
        for (int i = 1; i <=5; i++) {
            /*try {
                //休眠
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                
                e.printStackTrace();
            }*/
            System.out.println("B"+i+"\t"+Thread.currentThread().getName());
        }
    }
}
package cn.c.happy;

public class Test {
public static void main(String[] args) throws InterruptedException {
    SleepThread s1=new SleepThread();
    s1.setDaemon(true);
    s1.start();
    //交替執行
    for (int i = 1; i <=5; i++) {
        if(i==3){
            s1.join();//  調用join方法   執行完畢后再執行其他方法
            
        }
        System.out.println("A"+i+"\t"+Thread.currentThread().getName());
    }
}
}

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()