2023领星Java后端开发笔试
一、填空题
1.一个线程OOM后,其它线程还能运行吗?
答:可以。
2.面向对象的三大特性
答:封装、继承、多态
3.实现线程的方式
答:
1.继承Thread类
2.实现Runnable、Callable接口
4.同一个类多个方法的方法名字相同,参数列表不同、参数个数不同、参数的数据类型不同
不同数据类型的参数顺序不同。这种叫
答:重载
5.( )是操作系统资源分配的基本单位,而( )是处理器任务调度和执行的基本单位。
答:进程\线程
二、简答题
1.Mysql设计
某主页需要展示数据,用Mysql数据库,其中主表上亿数据,剩下2张表上百万数据,设计方案,让查询效率快,并且需要考虑少量的写场景。可以用到中间件。
答:看到一篇文章讲述的很好,这里放出链接
解决方案
三、编程题
1.倒置字符串
牛客:NC103 反转字符串
牛客-反转字符串
(1).二分法(最优)
public String solve6(String str) {
int length = str.length();
if (length == 0) {
return "";
}
char[] chars = str.toCharArray();
for (int i = 0; i < length / 2; i++) {
char temp = chars[i];
chars[i] = chars[length - 1 - i];
chars[length - 1 - i] = temp;
}
return new String(chars);
}
(2).栈 + StringBuffer
public String solve5(String str) {
int length = str.length();
if (length == 0) {
return "";
}
Stack<Character> stack = new Stack<>();
for (int i = 0; i < length; i++) {
stack.push(str.charAt(i));
}
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < length; i++) {
stringBuffer.append(stack.pop());
}
return stringBuffer.toString();
}
(3).利用String的toCharArray方法
public String solve(String str) {
int length = str.length();
if (length == 0) {
return "";
}
char[] chars = str.toCharArray();
String result = "";
for (int i = length - 1; i >= 0; i--) {
result += chars[i];
}
return result;
}
(4).利用StringBuffer
public String solve(String str) {
int length = str.length();
if (length == 0) {
return "";
}
return new StringBuffer(str).reverse().toString();
}
(5).正常
public String solve2(String str) {
int length = str.length();
if (0 == length) {
return "";
}
String result = "";
for (int i = length - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
}
2.两个线程交替运行
(1)方法一:用Synchronize+ wait() + notifyAll()
public class ThreadExercise2 {
private static String str1 = "ABCD";
private static String str2 = "1234";
public static void main(String[] args) throws InterruptedException {
Object lock = new Object();
MyThreadEx myth1 = new MyThreadEx(lock, str1);
MyThreadEx myth2 = new MyThreadEx(lock, str2);
myth1.start();
myth2.start();
myth1.join();
myth2.join();
}
}
class MyThreadEx extends Thread {
// lock是2个线程需要抢占的变量
private Object lock;
private String str;
public MyThreadEx(Object lock, String str) {
this.lock = lock;
this.str = str;
}
@Override
public void run() {
int length = str.length();
// 用synchronized关键字当互斥锁,锁住该对象
synchronized (this.lock) {
for (int i = 0; i < length; i++) {
System.out.println("线程" + Thread.currentThread().getId() + " : " + str.charAt(i));
// 这里注意notify会解除阻塞
lock.notify();
try {
// wait与synchronized一起使用会线程阻塞
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
(2)方法二:ReentrantLock+ Condition + await() + signal()
public class ThreadExercise3 {
private static String str1 = "ABCD";
private static String str2 = "1234";
private static ReentrantLock reentrantLock = new ReentrantLock();
public static void main(String[] args) throws InterruptedException {
Condition condition1 = reentrantLock.newCondition();
Condition condition2 = reentrantLock.newCondition();
LockEx lockEx1 = new LockEx(str1, reentrantLock, condition1, condition2);
Thread thread1 = new Thread(lockEx1);
LockEx lockEx2 = new LockEx(str2, reentrantLock, condition2, condition1);
Thread thread2 = new Thread(lockEx2);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
}
}
class LockEx implements Runnable {
private String str;
private ReentrantLock lock;
private Condition condition;
private Condition other;
public LockEx(String str, ReentrantLock lock, Condition condition, Condition other) {
this.str = str;
this.lock = lock;
this.condition = condition;
this.other = other;
}
@Override
public void run() {
int length = str.length();
lock.lock();
try {
for (int i = 0; i < length; i++) {
System.out.println("线程" + Thread.currentThread().getId() + ":" + str.charAt(i));
other.signalAll();
condition.await();
}
other.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
3.数组升序排列
这里排序算法都行,以冒泡为例
public class Bubble {
@Test
public void test() {
int nums[] = {1, 2, 3, 4, 5, 6};
bubble(nums);
System.out.println("最终是:" + Arrays.toString(nums));
}
public void bubble(int nums[]) {
int length = nums.length;
boolean change = false;
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
swap(nums, j, j + 1);
change = true;
}
}
System.out.println("第" + (i + 1) + "次循环后结果是:" + Arrays.toString(nums));
if (change == false) {
break;
} else {
change = true;
}
}
}
public void swap(int nums[], int x, int y) {
int temp = nums[x];
nums[x] = nums[y];
nums[y] = temp;
}
}