news 2026/2/11 5:43:32

Java 面试:10 个手写代码题(单例、排序、线程池等)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java 面试:10 个手写代码题(单例、排序、线程池等)

以下是10个Java面试常见手写代码题的实现,涵盖单例模式、排序算法、线程池等核心知识点:

单例模式(双重校验锁)

public class Singleton { private volatile static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } }

快速排序

public void quickSort(int[] arr, int low, int high) { if (low < high) { int pivot = partition(arr, low, high); quickSort(arr, low, pivot - 1); quickSort(arr, pivot + 1, high); } } private int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; swap(arr, i, j); } } swap(arr, i + 1, high); return i + 1; } private void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }

线程池实现

public class CustomThreadPool { private final BlockingQueue<Runnable> taskQueue; private final List<WorkerThread> workers; public CustomThreadPool(int poolSize) { this.taskQueue = new LinkedBlockingQueue<>(); this.workers = new ArrayList<>(); for (int i = 0; i < poolSize; i++) { WorkerThread worker = new WorkerThread(); worker.start(); workers.add(worker); } } public void execute(Runnable task) { taskQueue.offer(task); } private class WorkerThread extends Thread { public void run() { while (true) { try { Runnable task = taskQueue.take(); task.run(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } }

生产者消费者模式

public class ProducerConsumer { private final Queue<Integer> queue = new LinkedList<>(); private final int capacity; public ProducerConsumer(int capacity) { this.capacity = capacity; } public void produce() throws InterruptedException { int value = 0; while (true) { synchronized (this) { while (queue.size() == capacity) { wait(); } queue.add(value++); notifyAll(); Thread.sleep(1000); } } } public void consume() throws InterruptedException { while (true) { synchronized (this) { while (queue.isEmpty()) { wait(); } int value = queue.poll(); notifyAll(); Thread.sleep(1000); } } } }

二分查找

public int binarySearch(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; }

反转链表

public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } return prev; }

LRU缓存实现

class LRUCache { class Node { int key, value; Node prev, next; Node(int key, int value) { this.key = key; this.value = value; } } private Map<Integer, Node> cache; private int capacity; private Node head, tail; public LRUCache(int capacity) { this.capacity = capacity; cache = new HashMap<>(); head = new Node(0, 0); tail = new Node(0, 0); head.next = tail; tail.prev = head; } public int get(int key) { if (!cache.containsKey(key)) return -1; Node node = cache.get(key); remove(node); add(node); return node.value; } public void put(int key, int value) { if (cache.containsKey(key)) { remove(cache.get(key)); } Node node = new Node(key, value); cache.put(key, node); add(node); if (cache.size() > capacity) { Node toRemove = tail.prev; remove(toRemove); cache.remove(toRemove.key); } } private void add(Node node) { Node next = head.next; head.next = node; node.prev = head; node.next = next; next.prev = node; } private void remove(Node node) { Node prev = node.prev; Node next = node.next; prev.next = next; next.prev = prev; } }

二叉树层序遍历

public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<>(); if (root == null) return result; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { int levelSize = queue.size(); List<Integer> currentLevel = new ArrayList<>(); for (int i = 0; i < levelSize; i++) { TreeNode node = queue.poll(); currentLevel.add(node.val); if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); } result.add(currentLevel); } return result; }

字符串转整数(atoi)

public int myAtoi(String s) { if (s == null || s.isEmpty()) return 0; int index = 0, sign = 1, total = 0; // 跳过空格 while (index < s.length() && s.charAt(index) == ' ') { index++; } // 处理符号 if (index < s.length() && (s.charAt(index) == '+' || s.charAt(index) == '-')) { sign = s.charAt(index) == '+' ? 1 : -1; index++; } // 转换数字 while (index < s.length()) { int digit = s.charAt(index) - '0'; if (digit < 0 || digit > 9) break; // 检查溢出 if (Integer.MAX_VALUE / 10 < total || (Integer.MAX_VALUE / 10 == total && Integer.MAX_VALUE % 10 < digit)) { return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } total = 10 * total + digit; index++; } return total * sign; }

实现阻塞队列

public class BlockingQueue<T> { private Queue<T> queue = new LinkedList<>(); private int capacity; private Lock lock = new ReentrantLock(); private Condition notFull = lock.newCondition(); private Condition notEmpty = lock.newCondition(); public BlockingQueue(int capacity) { this.capacity = capacity; } public void put(T element) throws InterruptedException { lock.lock(); try { while (queue.size() == capacity) { notFull.await(); } queue.add(element); notEmpty.signal(); } finally { lock.unlock(); } } public T take() throws InterruptedException { lock.lock(); try { while (queue.isEmpty()) { notEmpty.await(); } T item = queue.remove(); notFull.signal(); return item; } finally { lock.unlock(); } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/9 8:06:39

Gemini 3 Deep Think:企业级部署的性能与成本天平上的舞者

引言 在当今数字化转型的浪潮中,企业对于人工智能技术的应用需求日益增长,Gemini 3 Deep Think 作为谷歌推出的新一代人工智能模型,以其卓越的性能和创新的架构,吸引了众多企业的目光。对于企业而言,在进行技术选型和部署时,成本与性能的平衡是至关重要的考量因素。Gemin…

作者头像 李华
网站建设 2026/2/6 18:08:31

亲测好用9个AI论文工具,本科生轻松搞定毕业论文!

亲测好用9个AI论文工具&#xff0c;本科生轻松搞定毕业论文&#xff01; AI 工具如何助力论文写作&#xff1f; 在如今的学术环境中&#xff0c;越来越多的学生开始借助 AI 工具来提升论文写作效率。对于本科生来说&#xff0c;撰写一篇高质量的毕业论文不仅需要扎实的专业知识…

作者头像 李华
网站建设 2026/2/7 4:45:18

一文说清ST7789的显存组织与像素映射机制

深入ST7789显存与像素映射&#xff1a;从寄存器到屏幕的每一帧如何精准呈现你有没有遇到过这种情况&#xff1f;明明代码里画的是一个方块&#xff0c;结果屏幕上却出来一段斜线&#xff1b;或者UI界面一旋转&#xff0c;文字就倒着显示、颜色发紫……这些问题&#xff0c;往往…

作者头像 李华
网站建设 2026/2/6 18:44:04

谁才是2026年远程控制领域的天花板?9大主流远程控制软件深度横评

在混合办公、跨地域协作、远程创作与云游戏全面普及的2026年&#xff0c;远程控制软件早已不再是IT运维的专属工具&#xff0c;而是成为数字生活中的“水电煤”——不可或缺、高频使用。 面对市场上琳琅满目的选择&#xff0c;用户最关心的问题只有一个&#xff1a;**谁才是真正…

作者头像 李华
网站建设 2026/2/10 4:09:08

【OTA专题】15 实现App后台无感下载固件

目录 软件架构 线程分配&#xff1a; OTA升级协议定义&#xff1a;​编辑 OTA升级流程图&#xff1a; 代码编写&#xff1a; OTA状态机搭建&#xff1a; OTA状态机&#xff1a; SWC_OTA.h文件定义枚举 宏与变量 搭建ota状态机线程 按键扫描函数 软复位函数 W25Q64&…

作者头像 李华