Interview

自我介绍

您好,我是谌先雄,来自湖北武汉,目前从事 Android 和 Flutter 客户端开发工作。过去几年,我在多个领域有着丰富的开发经验,包括物联网、金融、车载和 SAAS 等行业。

我使用 Java、Kotlin 和 Dart都有多年时间,有着足够的熟练度,在工作中,我参与了多个大型项目的开发,比如岚图 APP,负责核心模块的开发与维护,尤其在多媒体控制、支付集成以及性能优化方面积累了不少经验。

我有良好的团队协作能力,并且始终保持学习的热情,关注新技术在实践中的应用。

JAVA

Android

以下是 Java 实现的 LRU Cache 示例代码

import java.util.*;

public class LRUCache<K, V> {
    private final int capacity;
    private final Map<K, Node<K, V>> cache;
    private Node<K, V> head, tail;

    private static class Node<K, V> {
        final K key;
        V value;
        Node<K, V> prev, next;

        Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }

    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.cache = new HashMap<>();
    }

    public V get(K key) {
        Node<K, V> node = cache.get(key);
        if (node == null) {
            return null;
        }
        moveToHead(node);
        return node.value;
    }

    public void put(K key, V value) {
        Node<K, V> node = cache.get(key);
        if (node == null) {
            node = new Node<>(key, value);
            cache.put(key, node);
            addNode(node);
        } else {
            node.value = value;
            moveToHead(node);
        }
        if (cache.size() > capacity) {
            removeTail();
        }
    }

    private void addNode(Node<K, V> node) {
        if (head == null) {
            head = tail = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    private void removeNode(Node<K, V> node) {
        if (node == head) {
            head = head.next;
            if (head != null) {
                head.prev = null;
            }
        } else if (node == tail) {
            tail = tail.prev;
            if (tail != null) {
                tail.next = null;
            }
        } else {
            node.prev.next = node.next;
            node.next.prev = node.prev;
        }
    }

    private void moveToHead(Node<K, V> node) {
        removeNode(node);
        addNode(node);
    }

    private void removeTail() {
        if (tail != null) {
            cache.remove(tail.key);
            removeNode(tail);
        }
    }
}

Java自定义实现一个线程池

import java.util.*;
import java.util.concurrent.*;

public class MyThreadPool {
    private int poolSize;
    private List<Worker> workers;
    private BlockingQueue<MyTask> taskQueue;

    public MyThreadPool(int poolSize, BlockingQueue<MyTask> taskQueue) {
        this.poolSize = poolSize;
        this.taskQueue = taskQueue;
        workers = new ArrayList<>();
        for (int i = 0; i < poolSize; i++) {
            Worker worker = new Worker();
            workers.add(worker);
            worker.start();
        }
    }

    public void submit(Runnable task) {
        MyTask myTask = new MyTask(task);
        taskQueue.offer(myTask);
    }

    public void shutdown() {
        for (Worker worker : workers) {
            worker.interrupt();
        }
    }

    private class Worker extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    MyTask task = taskQueue.take();
                    task.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private class MyTask implements Runnable {
        private Runnable task;

        public MyTask(Runnable task) {
            this.task = task;
        }

        @Override
        public void run() {
            task.run();
        }
    }
}

源码理解

© 2019 - 2025 · Home · Theme Simpleness Powered by Hugo ·