K20230111 - JAVA -序列化类 + 通过控制台输入使线程结束

问题1

public class q1 {
    // 1、保存5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
    public static void main(String[] args) throws Exception {
        Student[] s = new Student[5];
        for (int i = 0; i < 5; i++)
            s[i] = new Student("小明" + i, Math.random() * 100, Math.random() * 100, Math.random() * 100);

        Arrays.sort(s, (a, b) -> (int) (b.cScore + b.eScore + b.mScore - a.eScore - a.mScore - a.cScore));

        FileOutputStream fos = new FileOutputStream("E:\\TEXT\\4.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        // Student implements Serializable  需要实现接口
        oos.writeObject(s);
        oos.close();
        fos.close();
    }
}

问题2

线程1
public class Thread01 extends Thread {

    @Override
    public void run() {
        int i = 0;
        while (true) {
            try {
                Thread.sleep(1000);// 打印速度慢点
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i++);
            i = i % 101;
        }
    }
}
线程2
public class Thread02 extends Thread{
    @Override
    public void run() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while(true){
            try {
                if (!!(s=br.readLine()).equals("Q")) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
main方法
public class q2 {
    /*
     * 2、打印数字,读取通知
     * (1)在main方法中启动两个线程;
     * (2)在1个线程循环打印100以内的整数;
     * (3)直到第2个线程从键盘读取了“Q”命令。
     */
    public static void main(String[] args) {
        Thread01 thread01 = new Thread01();
        thread01.setDaemon(true);//守护线程
        thread01.start();
        Thread02 thread02 = new Thread02();
        thread02.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值