加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[javaSE] 练习队列线程和对象序列化

发布时间:2021-05-21 06:45:28 所属栏目:大数据 来源: https://www.jb51.cc
导读:主要练习了队列数据结构,对象序列化和反序列化,多线程操作 import java.io.BufferedReader; java.io.File; java.io.FileInputStream; java.io.FileOutputStream; java.io.InputStream; java.io.InputStreamReader; java.io.ObjectInputStream; java.io.Obj

主要练习了队列数据结构,对象序列化和反序列化,多线程操作

import java.io.BufferedReader;
 java.io.File;
 java.io.FileInputStream;
 java.io.FileOutputStream;
 java.io.InputStream;
 java.io.InputStreamReader;
 java.io.ObjectInputStream;
 java.io.ObjectOutputStream;
 java.io.Serializable;

public class QueueThreadDemp {
    static StringQueue names;

    static void main(String[] args) throws Exception {
        names=getQueueObj();
        // 子线程循环队列
        new Thread(new Runnable() {
            @Override
            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 =  InputStreamReader(is);
        BufferedReader br =  BufferedReader(isr);

        ) {
            String name = br.readLine();
            names.push(name);
            
            File file = new File("E:queue.txt");
            FileOutputStream fos =  FileOutputStream(file);
            ObjectOutputStream oos =  ObjectOutputStream(fos);
            oos.writeObject(names);
            oos.close();
            fos.close();
        }

    }

    static StringQueue getQueueObj()  Exception {
        File file = );
        if (file.exists() && file.length() > 0) {
            FileInputStream fis =  FileInputStream(file);
            ObjectInputStream ois =  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;
    int nums;

    public StringQueue( nums) {
        mNames =  String[nums];
        this.nums = nums;
    }

     push(String name) {
        if (index >= nums) {
            pop();
        }
        mNames[index] = name;
        index++;
    }

     pop() {
        for (int i = 1; i < index; i++) {
            mNames[i - 1] = mNames[i];
        }
        index = index - 1public String toString() {
        String mes = "";
        for (String name : mNames) {
            mes += name + ",";
        }
         mes;
    }
}

效果:

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读