zoukankan      html  css  js  c++  java
  • 多线程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();
    }*/
    }

    }

  • 相关阅读:
    2018年12月9日 带小苗苗打针 函数2 前向引用 函数即变量
    2018年12月8日 函数变量与递归
    2018年12月7日 字符串格式化2 format与函数1
    2018年12月6日 字符串拼接 %的用法
    2018年11月29日 16点50分 小苗苗出生了
    2018年11月27日 分类与集合
    2018年11月26日 练习3
    2018年11月25日 练习2
    2018年11月24日 周末学习1 字典2
    2018年11月22日 字典 E18灯翼平整度 D&G is SB
  • 原文地址:https://www.cnblogs.com/JavaBlackHole/p/7662766.html
Copyright © 2011-2022 走看看