版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangfei0904306/article/details/79346312
同事那拷来的,据说是菜鸟的笔试题
* 把一个集合数据CP到一个新集合中
* 1,输入使用时间
* 2,把集合打印出来
* 3,多线程
* <p>
* 40分钟
同事的答案:
* 把一个集合数据CP到一个新集合中
* 1,输入使用时间
* 2,把集合打印出来
* 3,多线程
* <p>
* 40分钟
public static List<String> work(List<String> source) {
long start = System.currentTimeMillis();
if (Objects.isNull(source) || source.isEmpty()) {
return new ArrayList<>();
}
int size = source.size();
List<String> target = new ArrayList<>(size);
int threadNum = THREAD_NUM;
if (size < THREAD_NUM) {
threadNum = size;
}
CountDownLatch latch = new CountDownLatch(threadNum);
int recordNum = (size / threadNum) + 1;
IntStream.range(0, threadNum).forEach(i -> {
List<String> work = source.stream().skip(i * recordNum).limit(recordNum).collect(Collectors.toList());
Thread thread = new Thread(new CopyThread(work, target, latch));
thread.start();
});
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("复制【" + size + "】个数据,使用【" + (end - start) + "】毫秒");
return target;
}
public static void print(List<String> source) {
if (Objects.isNull(source)) {
return;
}
source.forEach(System.out::println);
}
static class CopyThread implements Runnable {
private List<String> source;
private List<String> target;
private CountDownLatch latch;
public CopyThread(List<String> source, List<String> target, CountDownLatch latch) {
this.source = source;
this.target = target;
this.latch = latch;
}
@Override
public void run() {
target.addAll(source);
latch.countDown();
}
}
附上某10年JAVA大牛整理的面试题:
1、基础(equals和hashcode,Map遍历,序列(serialVersionUID),ArrayList和LinkedList,clone,finally中return,redis数据类型,rabbitmq路由方式,cookie和session,常用工具类)
2、spring(Aop、IOC、MVC原理、Spring中自动装配的方式,spring cloud 负载,jwt)
3、数据库(事务、索引、死锁)
4、多线程(多线程程序有几种实现方式,threadlocal,synchronized和lock,当一个线程进入一个对象的synchronized方法A之后,其它线程是否可进入此对象的synchronized方法B)
5、设计(面向对象的原则、设计模式、设计方法(设计的产出物),倒索引,举例说明高并发系统的设计)
6、项目管理(软件开发的模式、敏捷开发的理解、测试驱动开发的理解、进度落后的处理)
7、单例模式,双重检查锁,ioc,aop,如何代理,rdis 数据类型,hashmap原理,负载均衡方式