面试记录

10月14日

问:工作中用到了哪些设计模式

单列模式,工厂模式,策略模式,模板模式,责任链模式

问:Java实现阻塞队列的新增(入队)和删除(出队)方法

这个出队方法没有写出来。

1
2
3
4
5
6
7
8
9
public class BlockingQueue { 
private final Queue queue = new LinkedList();
public void add(T item) {
queue.add(item);
}
public T poll() {
return queue.poll();
}
}

问:Java获取注解的简略代码

1
2
3
4
Class c = obj.class;
c.getAnnotation(注解.class);
method = c.getMethod(方法名,参数类型[]);
method.getAnnotation(注解.class);

问:快速排序算法

没有答出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 快速排序的递归实现  
public static void quickSort(int[] array, int left, int right) {
if (left < right) {
// 找到分区点
int pi = partition(array, left, right);

// 递归地对分区点左侧的子数组进行排序
quickSort(array, left, pi - 1);

// 递归地对分区点右侧的子数组进行排序
quickSort(array, pi + 1, right);
}
}

// 分区方法,用于重新排列数组并返回分区点
public static int partition(int[] array, int low, int high) {
int pivot = array[high]; // 选择最右边的元素作为基准
int i = (low - 1); // i是较小元素的索引

for (int j = low; j < high; j++) {
// 如果当前元素小于或等于基准
if (array[j] <= pivot) {
i++; // 增加较小元素的索引

// 交换array[i]和array[j]
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

// 交换array[i + 1]和array[high](或基准)
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;

return i + 1; // 返回分区点
}

问:不使用synchronized同步锁,怎么实现多线程共享变量

  1. 通过原子类实现:AtomicInteger、AtomicLong、AtomicReference
  2. 通过redis实现: set NX

问:t1,t2,t3三个线程,t3等待t1和t2执行完成后执行

面试题中必有的题目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Thread t1 = new Thread(() -> {  
System.out.println("t1 执行完成");
});

Thread t2 = new Thread(() -> {
System.out.println("t2 执行完成");
});

Thread t3 = new Thread(() -> {
t1.join();
t2.join();
System.out.println("t3 执行完成");
});

t1.start();
t2.start();
t3.start();

问:Linux常用命令,Java jvm优化命令

  • Linux 常用命令
  1. ps
  2. top
  3. find
  4. cp/mv/rm
  5. pwd
  • jvm优化命令
  1. jps
  2. jmap
  3. jstat
  4. jinfo
  5. -Xms(初始化堆大小)/-Xmx(最大堆大小)/-Xmn(新生代内存大小)
  6. -XX:PermSize(永久代初始值)/-XX:MaxPermSize(永久代最大值)