[javaSE] 練習隊列線程和對象序列化,javase序列化

來源:互聯網
上載者:User

[javaSE] 練習隊列線程和對象序列化,javase序列化

主要練習了隊列資料結構,對象序列化和還原序列化,多線程操作

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class QueueThreadDemp {    public static StringQueue names;    public static void main(String[] args) throws Exception {        names=getQueueObj();        // 子線程迴圈隊列        new Thread(new Runnable() {            @Override            public void run() {                while (true) {                    try {                        Thread.sleep(5000);                        names = QueueThreadDemp.getQueueObj();                        System.out.println(names.toString());                    } catch (Exception e) {                        e.printStackTrace();                    }                }            }        }).start();        // 讀取鍵盤輸入        InputStream is = System.in;        InputStreamReader isr = new InputStreamReader(is);        BufferedReader br = new BufferedReader(isr);        while (true) {            String name = br.readLine();            names.push(name);                        File file = new File("E:\\queue.txt");            FileOutputStream fos = new FileOutputStream(file);            ObjectOutputStream oos = new ObjectOutputStream(fos);            oos.writeObject(names);            oos.close();            fos.close();        }    }    public static StringQueue getQueueObj() throws Exception {        File file = new File("E:\\queue.txt");        if (file.exists() && file.length() > 0) {            FileInputStream fis = new FileInputStream(file);            ObjectInputStream ois = new ObjectInputStream(fis);            names = (StringQueue) ois.readObject();            ois.close();            fis.close();        } else {            names = new StringQueue(5);        }        return names;    }}//數組隊列class StringQueue implements Serializable {    private String[] mNames;    private int index = 0;    private int nums;    public StringQueue(int nums) {        mNames = new String[nums];        this.nums = nums;    }    public void push(String name) {        if (index >= nums) {            pop();        }        mNames[index] = name;        index++;    }    public void pop() {        for (int i = 1; i < index; i++) {            mNames[i - 1] = mNames[i];        }        index = index - 1;    }    public String toString() {        String mes = "";        for (String name : mNames) {            mes += name + ",";        }        return mes;    }}

效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.