面试-03

88 阅读2分钟
1.有序数组原地去重,要求不重复的数放在前部且有序,重复的数字放在后部,不要求顺序但必须保留。例如:1,2,3,3,4,5,6,6  -> 1,2,4,5,6,6,3,3

将f方法中的m1,m2,m3使用并行化执行,返回执行最快的result

 public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         *   //f()(
         *   //{
         *   // result = m1 //3ms
         *   // result = m2 //2ms
         *   // result = m3 //1ms
         *   //}
         *   //将f方法中的m1,m2,m3使用并行化执行
         *   //返回执行最快的result
         * 
         * @param args
         * @throws ExecutionException
         * @throws InterruptedException
         */

        // 初始化5个异步任务
        List<CompletableFuture<String>> futures = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            int a = i;
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                System.out.println("任务" + a + "开始 [ " + Thread.currentThread().getName());
                try {
                    Thread.sleep(new Random().nextInt(5) * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务" + a + "结束 [ " + Thread.currentThread().getName());
                return "任务" + a;
            });
            futures.add(future);
        }
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futures.toArray(new CompletableFuture[futures.size()]));
        Object result = anyOf.get(); // 阻塞等待任意一个任务结束
        System.out.println("最先执行结束的线程的结果为: " + result); // 返回那一个先结束任务的结果

        // 依次获得其他任务结果
        for (int i = 0; i < 3; i++) {
            System.out.println("任务" + i + "的结果为: " + futures.get(i));
        }
    }

索引:

如何避免回表现象:

  • 覆盖索引

索引失效 www.51cto.com/article/702…

  • 联合索引不满足最左匹配原则
  • 索引列参与运算:如explain select * from t_user where id + 1 = 2 ;
  • 索引列参使用了函数示:如explain select * from t_user where SUBSTR(id_no,1,3) = '100';
  • like以通配符开头('%abc')
  • 类型隐式转换,字符串不加单引号索引失效,解决方案(加上单引号或双引号)
  • or连接 (在使用or关键字时,切记两个条件都要添加索引,否则会导致索引失效)
    • or两边有一个字段没有创建索引
    • or两边为“>”和“<”范围查询时,索引失效。
  • 两列做比较:如explain select * from t_user where id > age;
  • order by 、group by 中有未加索引字段
  • 使用不等于(!=、<>)
    • 的时候无法使用索引会导致全表扫描(除覆盖索引外)
  • 索引范围条件右边的索引列会失效
  • 参数不同导致索引失效
    • >、< 、>=、<=、in等,当查询条件为大于等于、in等范围查询时,根据查询结果占全表数据比例的不同,优化器有可能会放弃索引,进行全表扫描。
  • 索引列参与运算
  • 索引列参使用了函数示例
  • is not null
  • not in和not exists
  • not in
    • 如果是主键则走索引,如果是普通索引,则索引失效。