[javaSE] 练习队列线程和对象序列化
2016-08-09 23:58
253 查看
主要练习了队列数据结构,对象序列化和反序列化,多线程操作
效果:
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; } }
效果:
相关文章推荐
- JavaSE第九十六讲:对象的序列化与反序列化深入讲解
- 黑马程序员-IO流(对象序列化、RandomAccessFile 类、字符编码、综合练习)
- Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
- 练习 2017-08-15 对象流 序列化和反序列化
- 利用对象的等待队列和锁队列管理线程。
- JavaSE学习 第十四章 高级IO编程和对象序列化
- Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
- Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
- JavaSE学习 第十四章 高级IO编程和对象序列化
- 17-集合框架-14-常用对象API(集合框架-LinkedList集合-练习(堆栈和队列))
- JAVA练习-第4天 对象流与序列化
- Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
- 安卓小日记练习(9)序列化对象List,通过文件读取在活动间传递数据
- JavaSE学习 第十四章 高级IO编程和对象序列化
- java 练习------创建线程,获取线程对象及名称,线程状态
- Javascript(二)-08-(常见对象-Array-练习-堆栈和队列结构)
- Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
- Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
- Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
- 黑马程序员--javaSE--对象序列化-字符串-Properties类