2021.8.1爱奇艺笔试第四题

题目意思是考察多线程编程,要求使用线程池,大部分代码直接给出了,让你实现Job类中的三个方法。

输入
aaa,bbb,ccc
输出
AAA,BBB,CCC


import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;


public class Main {


    public static void main(String[] args) throws InterruptedException {
        Solution s = new Solution();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(10000));
        final Scanner reader = new Scanner(System.in);
        final String next = reader.next();
        List<Line> lines = Arrays.stream(next.split(",")).map(str -> new StringLine(str))
                .collect(Collectors.toList());
        List<Line> result = s.translateAll(lines, "", threadPoolExecutor);
        String resultString = result.stream().map(l -> l.toString()).collect(Collectors.joining(","));
        System.out.println(resultString);
//        System.out.println(next.toUpperCase());  // 这么整才AC
        reader.close();
        threadPoolExecutor.shutdown();
    }

    public interface Line {
        /**
         * translate the line to the specific language
         *
         * @param language - the language to translate
         * @return the line of translated by the {@code language}
         */
        Line translate(String language);
    }

    public static class Solution {
        /**
         * translate the all lines to the specific language
         *
         * @param lines    the text lines of episode
         * @param language the language to translate
         * @return the lines of translated by the {@code language}
         */
        public List<Line> translateAll(List<Line> lines, String language, Executor executor) throws InterruptedException {
            Job<Line> job = new Job<>();
            for (Line line : lines) {
                Callable<Line> callable = () -> line.translate(language);
                job.newTask(callable);
            }
            job.execute(executor);
            return job.get();
        }
    }

    public static class Job<V> {
        List<V> result = new ArrayList<>();
        List<FutureTask<V>> futureTasks = new LinkedList<>();

        public void newTask(Callable<V> runnable) {
        	//待实现
            futureTasks.add(new FutureTask<V>(runnable));
        }


        public void execute(Executor executor) {
            //待实现
            for (FutureTask<V> f : futureTasks) {
                executor.execute(f);
            }
        }

        public List<V> get() throws InterruptedException {
            //待实现
            try {
                for (FutureTask<V> f: futureTasks) {
                    V v = f.get();
                    result.add(v);
                }
                return result;
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            return null;
        }

    }

    /**
     * translate the string line to upper case
     */
    public static class StringLine implements Line {
        private String text;

        public StringLine(String text) {
            this.text = text;
        }

        @Override
        public Line translate(String language) {
            return new StringLine(text.toUpperCase());
        }


        @Override
        public String toString() {
            return text;
        }
    }
}

只是最后提交本代码总是超时。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值