Java 使用对象流以及链表//进程调度

使用对象流和链表例子

此例子采用到的知识点

  1. 使用对象流时需要对象实现Serializable接口
  2. JPanel面板的使用
  3. 泛型类链表对象的使用以及遍历
  4. 盒子布局的相关
  5. 对象流的输入与输出
  6. JOptionPane类直接调用对话框
  7. 表格组件的使用

首先创建一个商品类 //切记切记最关键的一点是实现Serializable接口

import java.io.Serializable;

public class Goods implements Serializable { //
    String name;
    String count;
    String price;

    public void setName(String name) {
        this.name = name;
    }
    public void setCount(String count) {
        this.count = count;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public String getCount() {
        return count;
    }
    public String getPrice() {
        return price;
    }
}

其次创建一个输入面板类

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.LinkedList;

public class inputArea extends JPanel implements ActionListener {
    File file = null;
    Box BigBox,box1,box2;
    JTextField name,count,price;
    JButton submit;
    LinkedList<Goods> goodsList;
    inputArea(File file){
        this.file = file;   //获取文件
        goodsList = new LinkedList<Goods>();
        name = new JTextField(15);
        count = new JTextField(15);
        price = new JTextField(15);
        submit = new JButton("录入");
        submit.addActionListener(this);
        box1 = Box.createVerticalBox();
        box1.add(new JLabel("输入名称"));
        box1.add(Box.createVerticalStrut(8));
        box1.add(new JLabel("输入库存"));
        box1.add(Box.createVerticalStrut(8));
        box1.add(new JLabel("输入单价"));
        box1.add(Box.createVerticalStrut(8));
        box1.add(new JLabel("点击录入"));
        box2 = Box.createVerticalBox();
        box2.add(name);
        box2.add(Box.createVerticalStrut(8));
        box2.add(count);
        box2.add(Box.createVerticalStrut(8));
        box2.add(price);
        box2.add(Box.createVerticalStrut(8));
        box2.add(submit);
        BigBox = Box.createHorizontalBox();
        BigBox.add(box1);
        BigBox.add(Box.createHorizontalStrut(10));
        BigBox.add(box2);
        add(BigBox);
    }
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if(file.exists()){  //如果文件存在的话
            try{
                FileInputStream fileInputStream = new FileInputStream(file);
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                goodsList = (LinkedList<Goods>)objectInputStream.readObject();  //获取对象
                fileInputStream.close();
                objectInputStream.close();
                Goods goods = new Goods();
                goods.setName(name.getText());
                goods.setCount(count.getText());
                goods.setPrice(price.getText());
                goodsList.add(goods);   //链表添加结点
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                objectOutputStream.writeObject(goodsList);
                objectOutputStream.close();
                //fileInputStream.close();
            }
            catch (Exception E){

            }
        }
        else{
            try{
                file.createNewFile();
                Goods goods = new Goods();
                goods.setName(name.getText());
                goods.setCount(count.getText());
                goods.setPrice(price.getText());
                goodsList.add(goods);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                objectOutputStream.writeObject(goodsList);  //将链表输出
                objectOutputStream.close();
            }
            catch (Exception E){

            }

        }
    }
}

然后再创建查看表格面板类

import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
import java.util.LinkedList;

public class showArea extends JPanel {
    JTable jTable;
    Object name[] = {"名称","库存","单价"},element[][];
    public showArea(){
        setLayout(new BorderLayout());  //设置布局,东西南北中
        jTable = new JTable();
        add(new JScrollPane(jTable),BorderLayout.CENTER);

    }
    public void show(LinkedList<Goods> goodsLinkedList){
        remove(jTable);
        element = new Object[goodsLinkedList.size()][name.length];
        jTable = new JTable(element,name);
        add(new JScrollPane(jTable),BorderLayout.CENTER);
        Iterator<Goods> iterator = goodsLinkedList.iterator();
        int i = 0;
        while(iterator.hasNext()){
            Goods goods = iterator.next();
            element[i][0] = goods.getName();
            element[i][1] = goods.getCount();
            element[i][2] = goods.getPrice();
            i++;
        }
        jTable.repaint();
    }
}

再然后创建界面类

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;

public class myWindow extends JFrame implements ActionListener {
    File file;
    JMenuBar jMenuBar;
    JMenu jMenu;
    JMenuItem showMessage, inputMessage;
    showArea showArea;
    inputArea inputArea;
    JPanel jPanel;
    CardLayout cardLayout;

    myWindow() {
        file = new File("C:\\Users\\Administrator\\Desktop", "仓库.txt");
        inputArea = new inputArea(file);
        showArea = new showArea();
        jMenuBar = new JMenuBar();
        jMenu = new JMenu("菜单");
        showMessage = new JMenuItem("查看");
        showMessage.addActionListener(this);
        inputMessage = new JMenuItem("录入");
        inputMessage.addActionListener(this);
        jMenu.add(showMessage);
        jMenu.add(inputMessage);
        jMenuBar.add(jMenu);
        setJMenuBar(jMenuBar);
        jPanel = new JPanel();
        cardLayout = new CardLayout();
        jPanel.setLayout(cardLayout);
        jPanel.add("查看", showArea);
        jPanel.add("录入", inputArea);
        add(jPanel, BorderLayout.CENTER);
        cardLayout.show(jPanel, "录入");
        setVisible(true);
        setBounds(400, 400, 500, 500);
        validate();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if (actionEvent.getSource() == inputMessage) {
            cardLayout.show(jPanel, "录入");
        } else if (actionEvent.getSource() == showMessage) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                LinkedList<Goods> goodsLinkedList = (LinkedList<Goods>) objectInputStream.readObject();
                fileInputStream.close();
                objectInputStream.close();
                cardLayout.show(jPanel, "查看");
                showArea.show(goodsLinkedList);
            } catch (Exception E) {
                System.out.print(E);
                JOptionPane.showMessageDialog(this, "没有信息", "提示对话框", JOptionPane.WARNING_MESSAGE);

            }
        }

    }
}

最后是测试类

public class Example13_4 {
    public static void main(String[] args) {
        myWindow myWindow = new myWindow();
    }
}

运行效果图
在这里插入图片描述
在这里插入图片描述

进程调度

进程调度也就是所谓的处理机调度,主要功能就是确定在什么时候分派处理机,并确定分给哪一个进程
一个作业从提交到完成需要经历高中低三级调度
1.高级调度 //系统中一个作业只需要经过一次高级调度
2.中级调度 //决定处于交换区中的哪个就就绪进程可以调入内存
3.低级调度 //又称为短程调度,进程调度

  • 调度方式
    1.可剥夺式 //当有更能高优先级的进程到时,强行将正在运行的进程所占用的CPU分配给高优先级的进程
    2.不可剥夺式 //需要等待

  • 进程调度算法
    1.先来先服务 //FCFS
    2.时间片轮转

  • 优先级调度
    1.静态优先级 //进程的优先级在创建时就已经确定好了,知道进程终止都不会改变
    2.动态优先级 //过程中还可以改变

  • 多级反馈调度 //是时间片轮转算法和优先级算法的综合发展

CET4P182

  • behave
  • planet
  • crystal
  • physicist
  • interval
  • shortly
  • racial
  • image
  • sack
  • operational
  • construction
  • giant
  • blade
  • organism
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值