Java 并发编程基础
创建线程的方式
Section titled “创建线程的方式”// 方式1:继承 Thread 类public class MyThread extends Thread { @Override public void run() { System.out.println("Thread running"); }}
// 方式2:实现 Runnable 接口public class MyRunnable implements Runnable { @Override public void run() { System.out.println("Runnable running"); }}
// 方式3:使用 Lambda 表达式Thread thread = new Thread(() -> System.out.println("Lambda thread"));线程生命周期
Section titled “线程生命周期”- NEW: 新建状态
- RUNNABLE: 可运行状态
- BLOCKED: 阻塞状态
- WAITING: 等待状态
- TIMED_WAITING: 计时等待状态
- TERMINATED: 终止状态
synchronized 关键字
Section titled “synchronized 关键字”// 同步方法public synchronized void increment() { count++;}
// 同步代码块public void increment() { synchronized(this) { count++; }}
// 静态方法同步public static synchronized void staticMethod() { // 类级别锁}ReentrantLock
Section titled “ReentrantLock”private final ReentrantLock lock = new ReentrantLock();
public void safeMethod() { lock.lock(); try { // 临界区代码 } finally { lock.unlock(); }}wait/notify
Section titled “wait/notify”public class ProducerConsumer { private final Queue<String> queue = new LinkedList<>(); private final int capacity = 10;
public synchronized void produce(String item) throws InterruptedException { while (queue.size() == capacity) { wait(); } queue.add(item); notifyAll(); }
public synchronized String consume() throws InterruptedException { while (queue.isEmpty()) { wait(); } String item = queue.poll(); notifyAll(); return item; }}ExecutorService
Section titled “ExecutorService”// 固定大小线程池ExecutorService fixedPool = Executors.newFixedThreadPool(10);
// 缓存线程池ExecutorService cachedPool = Executors.newCachedThreadPool();
// 单线程池ExecutorService singlePool = Executors.newSingleThreadExecutor();
// 推荐:使用 ThreadPoolExecutor 自定义ThreadPoolExecutor executor = new ThreadPoolExecutor( 5, // 核心线程数 10, // 最大线程数 60L, // 空闲线程存活时间 TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), // 工作队列 new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略);并发编程是 Java 开发中的核心技能,需要理解线程安全、锁机制和线程池的使用。