碰到一个线程的算法,题目为java
某公司组织年会,会议入场时有两个入口,在入场时每位员工都能获取一张双色球彩票,假设公司有100个员工。 * 利用多线程模拟年会入场过程,并分别统计每一个入口入场的人数,以及每一个员工拿到的彩票的号码。 * 线程运行后打印格式以下: * 编号为: 2 的员工 从后门 入场! 拿到的双色球彩票号码是: [17, 24, 29, 30, 31, 32, 07] * 编号为: 1 的员工 从后门 入场! 拿到的双色球彩票号码是: [06, 11, 14, 22, 29, 32, 15] * //..... * 从后门入场的员工总共: 13 位员工 * 从前门入场的员工总共: 87 位员工
当时的想法就是同步输出:代码以下,认为有不少的瑕疵:算法
package com.hsm.mythread; public class EntryThread implements Runnable { static int i=1; static int a1=0; static int a2=0; static ThreadLocal<Integer> sum=new ThreadLocal<Integer>(); @Override public void run() { while(true){ synchronized(this){ if("1".equals(Thread.currentThread().getName())){ a1++; System.out.println("编号为: "+(i++) +"的员工 从后门 入场!"); }else{ a2++; System.out.println("编号为: "+(i++) +"的员工 从前门 入场!"); } if(i>=101){ if("1".equals(Thread.currentThread().getName())){ System.out.println("从后门入场的员工总共: "+a1+"位员工"); }else{ System.out.println("从后门入场的员工总共: "+a2+"位员工"); } break; } } } } } package com.hsm.mythread; import java.util.ArrayList; import java.util.Random; public class MultiThread { static Random rd = new Random(); public static void main(String[] args) { EntryThread a1=new EntryThread(); EntryThread a2=new EntryThread(); Thread b1=new Thread(a1, "1"); Thread b2=new Thread(a2, "2"); b1.start(); b2.start(); } }
结果以下:多线程
编号为: 98的员工 从前门 入场! 编号为: 99的员工 从后门 入场! 编号为: 100的员工 从前门 入场! 从后门入场的员工总共: 41位员工 从后门入场的员工总共: 59位员工
查看了别人的代码为dom
package com.hsm.mythread; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.Random; public class Test03 { /** * 某公司组织年会,会议入场时有两个入口,在入场时每位员工都能获取一张双色球彩票,假设公司有100个员工。 * 利用多线程模拟年会入场过程,并分别统计每一个入口入场的人数,以及每一个员工拿到的彩票的号码。 * 线程运行后打印格式以下: * 编号为: 2 的员工 从后门 入场! 拿到的双色球彩票号码是: [17, 24, 29, 30, 31, 32, 07] * 编号为: 1 的员工 从后门 入场! 拿到的双色球彩票号码是: [06, 11, 14, 22, 29, 32, 15] * //..... * 从后门入场的员工总共: 13 位员工 * 从前门入场的员工总共: 87 位员工 */ public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(); for (int i = 1; i <= 10; i++) { al.add(i); } GoIn.list = al; GoIn gi1 = new GoIn(); gi1.setName("前门"); GoIn gi2 = new GoIn(); gi2.setName("后门"); gi1.start(); gi2.start(); } } class GoIn extends Thread { public static ArrayList<Integer> list; static Random rd = new Random(); int count; public void run() { while(true) { synchronized (GoIn.class) { if(list.size() == 0){ //从后门入场的员工总共: 13 位员工 System.out.println("从"+Thread.currentThread().getName()+"入场的员工总共: "+ count +"位员工"); break; }else { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } int index = rd.nextInt(list.size()); int i = list.remove(index); System.out.println(i); count++; //编号为: 2 的员工 从后门 入场! 拿到的双色球彩票号码是: [17, 24, 29, 30, 31, 32, 07] System.out.println("编号为: "+ i +"的员工 从"+Thread.currentThread().getName()+"入场! 拿到的双色球彩票号码是:" + getDoubleColor()); } } } } private static String getDoubleColor() { LinkedHashSet<Integer> lhs = new LinkedHashSet<Integer>(); while(lhs.size() < 7){ lhs.add(rd.nextInt(32) + 1); } return lhs.toString(); } }
找到了不少的优点的地方,有一个地方是精髓ide
int j=0; ArrayList<Integer> al = new ArrayList<Integer>(); for (int i = 1; i <= 10; i++) { al.add(i); } for(int i=0;i<10;i++){ int index = rd.nextInt(al.size());//随机取list长度的索引 j=al.remove(index);//去掉索引对应的值 System.out.println(j);//输出结果 }
这里也是我想要总结的地方.这里能够用于输出.this