JAVA进阶知识大全(一)

简介: 教程来源 https://app-abggx9rbr6dd.appmiaoda.com 梳理Java进阶核心知识:深入JVM内存模型、GC机制与类加载;详解并发编程——线程生命周期、synchronized/ReentrantLock/原子类、CountDownLatch等JUC工具;对比BIO/NIO,解析网络编程与零拷贝。助你突破瓶颈,成为高效Java开发者。

当你已经掌握了Java的基础语法,能够编写简单的面向对象程序时,真正的挑战才刚刚开始。Java进阶开发要求你深入理解JVM原理、掌握并发编程、精通设计模式、熟悉企业级开发框架。本文将系统梳理Java进阶开发的核心知识体系,涵盖JVM底层、并发编程、网络编程、数据库访问、框架原理、性能优化等关键领域,助你突破瓶颈,成为一名真正高效的Java开发者。
ac465a36-7ed3-4025-8f1c-13f129adee64.png

一、JVM深入:理解Java的运行基石
1.1 JVM内存模型
JVM的内存结构是Java进阶的第一道门槛,理解内存划分对性能调优和问题排查至关重要。

/**
 * JVM内存结构
 * - 堆(Heap):存储对象实例,线程共享
 * - 方法区(Method Area):存储类信息、常量、静态变量,线程共享
 * - 虚拟机栈(VM Stack):存储局部变量、操作数栈,线程私有
 * - 本地方法栈(Native Method Stack):为Native方法服务
 * - 程序计数器(Program Counter):记录当前线程执行的字节码行号
 */
public class JVMMemoryDemo {
    // 静态变量存储在方法区
    private static int staticVar = 10;

    // 常量存储在方法区的运行时常量池
    private static final String CONSTANT = "Hello";

    public static void main(String[] args) {
        // 局部变量存储在虚拟机栈
        int localVar = 20;

        // 对象存储在堆中
        JVMMemoryDemo obj = new JVMMemoryDemo();

        // 数组也是对象,存储在堆中
        int[] arr = new int[100];
    }
}

1.2 垃圾回收机制
GC是Java自动内存管理的核心,理解GC算法和收集器是性能调优的基础。

import java.lang.ref.*;
import java.util.*;

/**
 * 垃圾回收基础
 * 可达性分析算法:从GC Roots开始,不可达的对象将被回收
 * GC Roots包括:
 * - 虚拟机栈引用的对象
 * - 方法区静态属性引用的对象
 * - 方法区常量引用的对象
 * - Native方法引用的对象
 */
public class GCDemo {

    // 演示引用类型
    public static void referenceTypes() {
        // 强引用:永不回收
        Object strongRef = new Object();

        // 软引用:内存不足时回收
        SoftReference<Object> softRef = new SoftReference<>(new Object());

        // 弱引用:下次GC时回收
        WeakReference<Object> weakRef = new WeakReference<>(new Object());

        // 虚引用:无法通过它获取对象,主要用于跟踪对象回收
        ReferenceQueue<Object> queue = new ReferenceQueue<>();
        PhantomReference<Object> phantomRef = new PhantomReference<>(new Object(), queue);

        // 手动触发GC(仅建议,不保证立即执行)
        System.gc();

        System.out.println("强引用:" + strongRef);
        System.out.println("软引用:" + softRef.get());
        System.out.println("弱引用:" + weakRef.get());
    }

    // 演示内存泄漏场景
    public static void memoryLeak() {
        // 场景1:静态集合持有对象引用
        List<Object> cache = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            cache.add(new byte[1024 * 1024]); // 每次添加1MB
            // 如果不清理,这些对象永远不会被回收
        }

        // 场景2:未关闭的资源
        // try (InputStream is = new FileInputStream("test.txt")) {
        //     // 自动关闭
        // }

        // 场景3:监听器未移除
        // component.addListener(listener);
        // 忘记 component.removeListener(listener);
    }
}

1.3 类加载机制
理解类加载过程,掌握双亲委派模型,是开发自定义类加载器和解决类冲突的基础。

/**
 * 类加载过程
 * 1. 加载:查找并加载类的二进制数据
 * 2. 验证:确保加载类的正确性
 * 3. 准备:为静态变量分配内存并初始化默认值
 * 4. 解析:将符号引用转换为直接引用
 * 5. 初始化:执行静态初始化块和静态变量赋值
 */
public class ClassLoaderDemo {

    // 演示类加载顺序
    static class Parent {
        static {
            System.out.println("Parent static block");
        }

        Parent() {
            System.out.println("Parent constructor");
        }
    }

    static class Child extends Parent {
        static {
            System.out.println("Child static block");
        }

        Child() {
            System.out.println("Child constructor");
        }
    }

    public static void main(String[] args) throws Exception {
        // 查看类加载器
        System.out.println("AppClassLoader: " + ClassLoaderDemo.class.getClassLoader());
        System.out.println("ExtClassLoader: " + ClassLoaderDemo.class.getClassLoader().getParent());
        System.out.println("BootstrapClassLoader: " + 
            ClassLoaderDemo.class.getClassLoader().getParent().getParent());

        // 演示类加载顺序
        System.out.println("=== 第一次创建对象 ===");
        new Child();
        System.out.println("=== 第二次创建对象 ===");
        new Child();

        // 自定义类加载器示例
        CustomClassLoader customLoader = new CustomClassLoader();
        Class<?> clazz = customLoader.loadClass("com.example.MyClass");
        System.out.println("加载的类:" + clazz.getName());
    }

    // 自定义类加载器
    static class CustomClassLoader extends ClassLoader {
        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            // 从自定义位置加载字节码
            byte[] classData = loadClassData(name);
            if (classData == null) {
                throw new ClassNotFoundException();
            }
            return defineClass(name, classData, 0, classData.length);
        }

        private byte[] loadClassData(String name) {
            // 实现从文件系统、网络等位置加载类字节码
            return null;
        }
    }
}

二、并发编程:驾驭多线程的力量
2.1 线程基础与生命周期

/**
 * 线程生命周期:NEW -> RUNNABLE -> BLOCKED/WAITING/TIMED_WAITING -> TERMINATED
 */
public class ThreadBasics {

    // 创建线程的两种方式
    public static void createThread() {
        // 方式1:继承Thread类
        Thread thread1 = new Thread() {
            @Override
            public void run() {
                System.out.println("Thread1 running");
            }
        };

        // 方式2:实现Runnable接口(推荐)
        Runnable task = () -> {
            System.out.println("Thread2 running");
        };
        Thread thread2 = new Thread(task);

        thread1.start();
        thread2.start();
    }

    // 线程状态演示
    public static void threadStates() throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(1000);  // TIMED_WAITING状态
                synchronized (ThreadBasics.class) {
                    ThreadBasics.class.wait();  // WAITING状态
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println("状态1: " + thread.getState());  // NEW
        thread.start();
        System.out.println("状态2: " + thread.getState());  // RUNNABLE

        Thread.sleep(500);
        System.out.println("状态3: " + thread.getState());  // TIMED_WAITING
    }
}

2.2 线程同步与锁

import java.util.concurrent.locks.*;
import java.util.concurrent.atomic.*;

/**
 * 线程同步机制
 * - synchronized关键字
 * - ReentrantLock可重入锁
 * - ReadWriteLock读写锁
 * - 原子变量
 */
public class SynchronizationDemo {

    // 1. synchronized示例
    public static class SynchronizedCounter {
        private int count = 0;

        // 同步方法
        public synchronized void increment() {
            count++;
        }

        // 同步代码块
        public void decrement() {
            synchronized (this) {
                count--;
            }
        }

        public synchronized int getCount() {
            return count;
        }
    }

    // 2. ReentrantLock示例
    public static class LockCounter {
        private final ReentrantLock lock = new ReentrantLock();
        private int count = 0;

        public void increment() {
            lock.lock();
            try {
                count++;
            } finally {
                lock.unlock();
            }
        }

        // 尝试获取锁(非阻塞)
        public boolean tryIncrement() {
            if (lock.tryLock()) {
                try {
                    count++;
                    return true;
                } finally {
                    lock.unlock();
                }
            }
            return false;
        }

        // 可中断的锁获取
        public void incrementWithInterrupt() throws InterruptedException {
            lock.lockInterruptibly();
            try {
                count++;
            } finally {
                lock.unlock();
            }
        }
    }

    // 3. ReadWriteLock读写锁
    public static class ReadWriteCounter {
        private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
        private final Lock readLock = rwLock.readLock();
        private final Lock writeLock = rwLock.writeLock();
        private int count = 0;

        public int getCount() {
            readLock.lock();
            try {
                return count;
            } finally {
                readLock.unlock();
            }
        }

        public void increment() {
            writeLock.lock();
            try {
                count++;
            } finally {
                writeLock.unlock();
            }
        }
    }

    // 4. 原子变量(无锁编程)
    public static class AtomicCounter {
        private final AtomicInteger count = new AtomicInteger(0);

        public void increment() {
            count.incrementAndGet();  // 原子操作
        }

        public int getCount() {
            return count.get();
        }
    }

    // 5. 死锁演示与避免
    public static class DeadlockDemo {
        private final Object lock1 = new Object();
        private final Object lock2 = new Object();

        public void method1() {
            synchronized (lock1) {
                System.out.println("method1获取lock1");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (lock2) {
                    System.out.println("method1获取lock2");
                }
            }
        }

        public void method2() {
            synchronized (lock2) {
                System.out.println("method2获取lock2");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (lock1) {
                    System.out.println("method2获取lock1");
                }
            }
        }

        // 解决方案:统一锁顺序、使用tryLock、使用更高层次的并发工具
    }
}

2.3 并发工具类

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

/**
 * JUC并发工具类
 * - CountDownLatch:等待多个线程完成
 * - CyclicBarrier:循环屏障,等待多个线程到达屏障点
 * - Semaphore:信号量,控制并发访问数量
 * - Exchanger:线程间交换数据
 * - CompletableFuture:异步编程
 */
public class ConcurrencyTools {

    // CountDownLatch示例:等待所有线程完成
    public static void countDownLatchDemo() throws InterruptedException {
        int threadCount = 5;
        CountDownLatch latch = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + " 开始工作");
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName() + " 完成工作");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    latch.countDown();  // 计数器减1
                }
            }).start();
        }

        latch.await();  // 等待计数器归零
        System.out.println("所有线程已完成,继续执行");
    }

    // CyclicBarrier示例:等待所有线程到达屏障点
    public static void cyclicBarrierDemo() {
        int parties = 3;
        CyclicBarrier barrier = new CyclicBarrier(parties, () -> {
            System.out.println("所有线程已到达屏障点,执行汇总操作");
        });

        for (int i = 0; i < parties; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + " 到达屏障");
                    barrier.await();
                    System.out.println(Thread.currentThread().getName() + " 继续执行");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    // Semaphore示例:限流
    public static void semaphoreDemo() {
        Semaphore semaphore = new Semaphore(3);  // 最多3个并发

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();  // 获取许可
                    System.out.println(Thread.currentThread().getName() + " 获取许可,执行业务");
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();  // 释放许可
                    System.out.println(Thread.currentThread().getName() + " 释放许可");
                }
            }).start();
        }
    }

    // CompletableFuture示例:异步编程
    public static void completableFutureDemo() {
        // 异步执行任务
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
                return "Hello";
            } catch (InterruptedException e) {
                return "Error";
            }
        }).thenApply(result -> result + " World")  // 转换结果
          .thenApply(String::toUpperCase)
          .exceptionally(throwable -> "Exception: " + throwable.getMessage());

        // 组合多个异步任务
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");

        CompletableFuture<String> combined = future1.thenCombine(future2, (s1, s2) -> s1 + " " + s2);

        // 等待结果
        try {
            System.out.println(future.get());
            System.out.println(combined.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // BlockingQueue示例:生产者-消费者模式
    public static void blockingQueueDemo() {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);

        // 生产者
        Thread producer = new Thread(() -> {
            try {
                for (int i = 0; i < 100; i++) {
                    queue.put(i);  // 队列满时阻塞
                    System.out.println("生产: " + i);
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        // 消费者
        Thread consumer = new Thread(() -> {
            try {
                while (true) {
                    Integer value = queue.take();  // 队列空时阻塞
                    System.out.println("消费: " + value);
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        producer.start();
        consumer.start();
    }
}

三、网络编程与NIO
3.1 BIO与NIO对比

import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;

/**
 * BIO(Blocking I/O):阻塞IO,每个连接一个线程
 * NIO(Non-blocking I/O):非阻塞IO,单线程处理多个连接
 * AIO(Asynchronous I/O):异步IO,回调机制
 */
public class NetworkProgramming {

    // BIO服务器示例
    public static class BIOServer {
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket = new ServerSocket(8080);
            System.out.println("BIO服务器启动,端口8080");

            while (true) {
                // 阻塞等待客户端连接
                Socket socket = serverSocket.accept();
                System.out.println("客户端连接:" + socket.getRemoteSocketAddress());

                // 每个连接创建新线程处理
                new Thread(() -> {
                    try (BufferedReader reader = new BufferedReader(
                            new InputStreamReader(socket.getInputStream()));
                         PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) {

                        String line;
                        while ((line = reader.readLine()) != null) {
                            System.out.println("收到: " + line);
                            writer.println("ECHO: " + line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }
    }

    // NIO服务器示例(Selector多路复用)
    public static class NIOServer {
        public static void main(String[] args) throws IOException {
            // 创建Selector
            Selector selector = Selector.open();

            // 创建ServerSocketChannel并注册到Selector
            ServerSocketChannel serverChannel = ServerSocketChannel.open();
            serverChannel.bind(new InetSocketAddress(8081));
            serverChannel.configureBlocking(false);
            serverChannel.register(selector, SelectionKey.OP_ACCEPT);

            System.out.println("NIO服务器启动,端口8081");

            // 事件循环
            while (true) {
                selector.select();  // 阻塞等待事件

                Set<SelectionKey> selectedKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = selectedKeys.iterator();

                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    iterator.remove();

                    if (key.isAcceptable()) {
                        // 接受新连接
                        ServerSocketChannel server = (ServerSocketChannel) key.channel();
                        SocketChannel client = server.accept();
                        client.configureBlocking(false);
                        client.register(selector, SelectionKey.OP_READ);
                        System.out.println("客户端连接:" + client.getRemoteAddress());

                    } else if (key.isReadable()) {
                        // 读取数据
                        SocketChannel client = (SocketChannel) key.channel();
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        int bytesRead = client.read(buffer);

                        if (bytesRead > 0) {
                            buffer.flip();
                            byte[] data = new byte[buffer.remaining()];
                            buffer.get(data);
                            String message = new String(data);
                            System.out.println("收到: " + message);

                            // 响应
                            ByteBuffer response = ByteBuffer.wrap(("ECHO: " + message).getBytes());
                            client.write(response);
                        } else if (bytesRead == -1) {
                            client.close();
                            System.out.println("客户端断开连接");
                        }
                    }
                }
            }
        }
    }

    // 零拷贝示例(transferTo)
    public static void zeroCopyDemo() throws IOException {
        // 传统方式:数据需要从内核空间复制到用户空间,再写回内核空间
        // 零拷贝:直接在内核空间传输,减少拷贝次数

        FileChannel source = FileChannel.open(Paths.get("source.txt"), StandardOpenOption.READ);
        FileChannel dest = FileChannel.open(Paths.get("dest.txt"), 
            StandardOpenOption.CREATE, StandardOpenOption.WRITE);

        // 零拷贝传输
        long transferred = source.transferTo(0, source.size(), dest);
        System.out.println("传输字节数:" + transferred);

        source.close();
        dest.close();
    }
}

来源:
https://app-abggx9rbr6dd.appmiaoda.com

相关文章
|
3月前
|
人工智能 机器人 API
OpenClaw多Agent架构搭建保姆级图文教程(阿里云、本地)+百炼大模型api配置实操指南
在2026年的AI Agent生态中,OpenClaw(原Clawdbot)的多Agent模式成为实现智能化任务协作的核心方案,单Agent的“万金油”模式已无法满足精细化的任务处理需求——将不同职能拆分至专属Agent,通过调度层实现任务分配与结果汇总,能有效避免AI幻觉、提升响应专业性与效率。本文将完整覆盖OpenClaw多Agent架构的核心搭建逻辑,同时补充2026年阿里云云端部署、本地MacOS/Linux/Windows11部署的实操流程,阿里云千问大模型与免费Coding Plan API的配置方法,结合飞书生态的多Agent联动配置,以及部署和搭建过程中的全量避坑指南与常见问题
1927 1
|
Java 数据库连接 mybatis
mybatis返回map类型数据空值字段不显示(三种解决方法)
mybatis返回map类型数据空值字段不显示(三种解决方法)
|
9天前
|
SQL 自然语言处理 数据库
为什么多数智能问数项目失败不是因为技术,而是因为业务没想清楚?
本文指出:智能问数项目失败的根因并非技术不足,而是业务定义不清——对象边界模糊、术语口径不一、复合逻辑缺失。本体语义层路线通过结构化表达对象、属性、关系及业务定义,实现“又泛又准”的问数能力;相较预置或NL2SQL路线,其维护成本更可控、扩展性更强。成功前提在于企业愿投入语义治理。
|
3月前
|
JavaScript Linux API
OpenClaw(Clawdbot)从零到一实操手册:云端本地双部署+技能扩展+问题一站式解决
OpenClaw(曾用名Clawdbot/Moltbot)是2026年主流开源AI智能体框架,支持本地与云端双模式部署,可通过自然语言完成文件管理、信息检索、内容处理、流程自动化等任务,并依托Skills扩展更多实用能力。本文基于2026年3月最新环境,完整提供阿里云轻量应用服务器极速部署、本地Windows11/macOS/Linux手动部署、阿里云百炼Coding Plan免费大模型API配置、常用技能集成方法,以及全流程高频问题排查方案,所有命令均可直接复制执行,零基础用户也可快速完成搭建与稳定运行。
761 0
|
4月前
|
监控 安全 网络安全
Charles网络抓包软件怎么用?完整安装与使用指南
Charles是一款轻量HTTP/HTTPS抓包工具,支持电脑、手机、模拟器等设备的网络请求监控与分析。安装简单:下载MSI包→双击运行→默认配置→完成即用。首次启动需允许代理设置,可即时捕获网页流量,助力开发调试与接口测试。(239字)
|
8月前
|
设计模式 网络协议 数据可视化
Java 设计模式之状态模式:让对象的行为随状态优雅变化
状态模式通过封装对象的状态,使行为随状态变化而改变。以订单为例,将待支付、已支付等状态独立成类,消除冗长条件判断,提升代码可维护性与扩展性,适用于状态多、转换复杂的场景。
1038 157
|
3月前
|
人工智能 API 项目管理
从平台来源到生态布局:对 GitCode 的背景与能力做一次中立介绍
GitCode是由CSDN与华为云CodeArts联合打造的新一代开源开发者平台,融合社区生态与研发基建,支持代码托管、协同开发、CI/CD及开源运营,深度对接开放原子等本土生态,致力于构建“代码+社区+AI”一体化开源基础设施。(239字)
437 0
|
3月前
|
人工智能 Linux iOS开发
OpenClaw+QMT‑MCP量化交易实战:AI交易员全流程部署、模型配置与自动交易实现(附阿里云/Windows/macOS/Linux部署OpenClaw教程)
在量化交易领域,自动化执行与策略智能化已成为主流方向。OpenClaw(Clawdbot)作为开源AI Agent框架,可充当交易系统的“大脑”,负责理解指令、分析行情、拆解逻辑、规划执行;QMT‑MCP则遵循MCP(Model Context Protocol)协议,将本地QMT交易客户端封装为标准接口,成为AI交易员的“执行双手”,完成下单、撤单、查询持仓、查询资产等真实交易操作。
2589 7
|
9月前
|
IDE 安全 Java
Lombok 在企业级 Java 项目中的隐性成本:便利背后的取舍之道
Lombok虽能简化Java代码,但其“魔法”特性易破坏封装、影响可维护性,隐藏调试难题,且与JPA等框架存在兼容风险。企业级项目应优先考虑IDE生成、Java Records或MapStruct等更透明、稳健的替代方案,平衡开发效率与系统长期稳定性。
577 115

热门文章

最新文章