• 多线程10人过山洞


    package com.atguigui.bean;

    import java.util.ArrayList;
    import java.util.LinkedHashSet;
    import java.util.LinkedList;
    import java.util.Set;
    import java.util.TreeSet;

    /**
    * 多线程过山车,山洞一次通过一个人,每个人通过山洞的时间为5秒,随机生成10个人, 同时准备过山洞,显示每次通过山洞的人的姓名
    *
    * @author Administrator
    *
    * 1. 随机生成10个人名, 2. 线程,要么继承,要么实现接口,3,睡5秒,间隔时间为5秒,4,同时准备
    */

    public class GuoShanDong implements Runnable {
    private int deng = 0;

    @Override
    public void run() {
    deng = deng + 100;
    // 当前线程睡眠5秒
    try {
    Thread.sleep(deng);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    // 输出哪个线程名,或者人名,在过山洞
    System.out.println(Thread.currentThread().getName() + " 过山洞");
    }

    public static void main(String[] args) {
    String arr[] = { "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈" }; //容器,数组,装10个人名。

    GuoShanDong gsd = new GuoShanDong(); //对象,调用线程方法的

    Set<Integer> set = new LinkedHashSet<>();
    //LinkedList<Integer> list = new LinkedList<>();
    while (true) {
    if (set.size() == 10) {
    break;
    }
    int a = (int) (Math.random() * 10);

    set.add(a);
    }

    for (int b : set) {

    Thread th = new Thread(gsd,arr[b]);
    th.start();
    }

    //Set<Integer> set = new LinkedHashSet<>();
    /*LinkedList<Integer> list = new LinkedList<>();
    while (true) {
    if (list.size() == 10) {
    break;
    }
    int a = (int) (Math.random() * 10);

    list.add(a);
    }

    for (int b : list) {

    Thread th = new Thread(gsd,arr[b]);
    th.start();
    }*/
    }

    }

  • 相关阅读:
    「WC2021」表达式求值
    [补]「WC2021」括号路径
    「CEOI2020」星际迷航
    「CEOI2018」斐波那契表示法
    CF913F
    CF1017G The Tree
    NOI2020 超现实树
    LOJ 6714 Stupid Product
    LOJ 575. 不等关系
    CF1267G
  • 原文地址:https://www.cnblogs.com/JavaBlackHole/p/7662766.html
Copyright © 2020-2023  润新知