粤港澳大湾区日常实习
1.代码优化题
//如下代码,compute是一个耗时操作,如果多个线程并发访问,可能会造成多个线程同时执行compute操作,如何进行优化?(完全不写题目,面试官也完全不说话,题意全靠自己猜...)
//优化思路:大致意思是需要同步加锁,但这个题意的不同的k是可以并发执行的,同一个k只需要执行一次compute即可,故这里应该加一个
public class Memoizer<A, V> implements Computable<A, V> { private final Map<A, V> cache = new ConcurrentHashMap<A, V>(); private final Map<A,F> flag; private final Computable<A, V> c; public Memoizer(Computable<A, V> c) { this.c = c; } @Override public V computer(A arg) throws InterruptedException { V result = cache.get(arg); if (result == null) { result = c.compute(arg); // 耗时操作 cache.put(arg, result); } return result; } } public interface Computable<A, V> { V compute(A arg) throws InterruptedException; }
public class Memoizer<A, V> implements Computable<A, V> {
private final Map<A, V> cache = new ConcurrentHashMap<A, V>();
private final Map<A,F> flag;
private final Computable<A, V> c;
public Memoizer(Computable<A, V> c) {
this.c = c;
}
public LockByKey lockByKey = new LockByKey();
@Override
public V computer(A arg) throws InterruptedException {
V result = cache.get(arg);
try{
lockByKey.lock(key);
if (result == null) {
result = c.compute(arg); // 耗时操作
cache.put(arg, result);
}
}finally{
lockByKey.unlock(key);
return result;
}
}
public interface Computable<A, V> {
V compute(A arg) throws InterruptedException;
}
public class LockByKey {
private static class LockWrapper {
private final Lock lock = new ReentrantLock();
private final AtomicInteger numberOfThreadsInQueue = new AtomicInteger(1);
private LockWrapper addThreadInQueue() {
numberOfThreadsInQueue.incrementAndGet();
return this;
}
private int removeThreadFromQueue() {
return numberOfThreadsInQueue.decrementAndGet();
}
}
private static ConcurrentHashMap<String, LockWrapper> locks = new ConcurrentHashMap<String, LockWrapper>();
public void lock(String key) {
LockWrapper lockWrapper = locks.compute(key, (k, v) -> v == null ? new LockWrapper() : v.addThreadInQueue());
lockWrapper.lock.lock();
}
public void unlock(String key) {
LockWrapper lockWrapper = locks.get(key);
lockWrapper.lock.unlock();
if (lockWrapper.removeThreadFromQueue() == 0) {
// NB : We pass in the specific value to remove to handle the case where another thread would queue right before the removal
locks.remove(key, lockWrapper);
}
}
}
2.sql语句优化
SELECT * FROM user AS a LEFT JOIN user_info AS b ON a.id = b.user_id where gender=1 AND age>20;
- select * 十分耗时,因为需要回表,并且io的数据量比较大
- 可以建立gender与age的联合索引,可以减少数据的扫描数量
- 将select * 改为所需要的属性后,可以根据多个属性建立联合索引,可以减少回表操作
- 建立联合索引是要注意顺序,在这里genger应放在前面,因为联合索引遇到范围查询会停止,其他需要返回的属性可以放在最后
- 可以将where的语句合并在on中
3.算法题--写一个快排
void quickSort(int[] nums,int l,int r){
//int l = 0, r = nums.size();
int mid = nums[(l+r)>>1];
int left = l-1, right = r+1;
while(left<right){
do left++; while(nums[left]<mid);
do right--; while(nums[right]>mid);
if(left<right){
swap(nums[left],nums[right]);
}
quickSort(nums,l,right);
quickSort(nums,right+1,r);
}
}
int main(){
int nums[] = {5,3,2,1};
quickSort(nums,0,nums.size());
return;
}
理想主义的花终将在现实中绽放

浙公网安备 33010602011771号