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; } }
点赞 评论

相关推荐

路过的周先森:我就喜欢这种无限复活+筛选快的,比那些投进去就没声还占用投递次数的好多了
投递快手等公司7个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
10-29 21:14
疯犬丨哈士奇:喜欢你的人会主动表白,对你有想法的人会很主动,所以要你的公司不会吊着你所以懂了吧
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务