import java.util.Scanner; import java.util.Vector; import java.util.concurrent.*; public class Main { public static void main(String[] args) throws ExecutionException, InterruptedException { Vector<String> queue=new Vector<String>(); CountDownLatch latch=new CountDownLatch(1); Produce produce = new Produce(queue,latch); ExecutorService service = Executors.newFixedThreadPool(4); service.execute(produce); Future<Integer> submit1 = service.submit(new Consumer(queue,latch)); Future<Integer> submit2 = service.submit(new Consumer(queue,latch)); Future<Integer> submit3 = service.submit(new Consumer(queue,latch)); System.out.println(submit1.get()+submit2.get()+submit3.get()); service.shutdown(); } } class Produce implements Runnable{ Vector<String> queue=null; Scanner sc=new Scanner(System.in); CountDownLatch latch; Produce(Vector<String> queue,CountDownLatch latch){ this.queue=queue; this.latch=latch; } @Override public void run() { while(sc.hasNext()){ String s = sc.nextLine(); if (s.equals("110")){break;} queue.add(s); } latch.countDown(); } } class Consumer implements Callable<Integer>{ Vector<String> queue; CountDownLatch latch; Consumer(Vector<String> queue,CountDownLatch latch){ this.queue=queue; this.latch=latch; } int count=0; @Override public Integer call() throws Exception { latch.await(); while(!queue.isEmpty()){ String s = queue.remove(0); if (s.contains("u51")){ count++; } } return count; } }
点赞 评论

相关推荐

无情咸鱼王的秋招日记之薛定谔的Offer:好拒信,偷了,希望有机会用到
点赞 评论 收藏
分享
牛客网
牛客企业服务