Today, a friend asked me an interview question. There were 5 people grabbing 5 red envelopes. They could be repeatedly grabbed and implemented using multi-threaded programs. There are many ways to implement them. I would like to share my ideas: the characteristics of blocking queues are applied.
/** * Created by zhanglinqiang on 2016/6/23. */ public class MyTest { public static void main(String[] args) throws InterruptedException { LinkedBlockingQueue<LuckyMoney> luckyMoneys = new LinkedBlockingQueue<>(); List<FutureTask> futureTasks = new ArrayList<>(); //Prepare to grab red envelopes for (int i = 0; i < 5; i++) { FutureTask<Object> futureTask = new FutureTask<>(new CatchLuckMoney(luckyMoneys, "name" + i), null); new Thread(futureTask,"name"+i).start(); futureTasks.add(futureTask); } Thread.sleep(5);//Make sure the thread of grabbing the red envelope is ready Random random = new Random(100); //Send 5 red envelopes for (int i = 0; i < 5; i++) { luckyMoneys.put(new LuckyMoney("red envelope"+i, random.nextInt(100)+1)); } //Wait until the red envelope is grabbed while (!luckyMoneys.isEmpty()){ Thread.sleep(1); } //Terminate the red envelope grab thread for (FutureTask futureTask : futureTasks) { futureTask.cancel(true); } } } class CatchLuckMoney implements Runnable { public CatchLuckMoney(LinkedBlockingQueue<LuckyMoney> luckyMoneys, String name) { this.luckyMoneys = luckyMoneys; this.name = name; } private LinkedBlockingQueue<LuckyMoney> luckyMoneys; private String name; @Override public void run() { while (!Thread.currentThread().isInterrupted()) { LuckyMoney redPackage = null; try { redPackage = luckyMoneys.take(); } catch (InterruptedException e) { // e.printStackTrace(); break; } System.out.println(name + "Grabbed-->" + redPackage); luckyMoneys.remove(redPackage); } System.out.println("end>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+Thread.currentThread().getName()); } } class LuckyMoney { public LuckyMoney(String name, Integer money) { this.name = name; this.money = money; } private String name; private Integer money; @Override public String toString() { return "LuckyMoney{" + "name='" + name + '/'' + ", money=" + money + '}'; } } Running effect:
Connected to the target VM, address: '127.0.0.1:8869', transport: 'socket' name3 got -->LuckyMoney{name='red envelope 0', money=16} name4 got -->LuckyMoney{name='red envelope 3', money=89} name0 got -->LuckyMoney{name='red envelope 2', money=75} name1 got -->LuckyMoney{name='red envelope 1', money=51} name3 got -->LuckyMoney{name='red envelope 4', money=92} end>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.