2026年物业门控五金耗材推荐榜:中企创联工业品,小区/写字楼/物业多场景门控配件全覆盖
2026/3/2 14:07:46
多线程是指在一个程序中同时运行多个线程,每个线程可以独立执行不同的任务。Java从语言层面提供了强大的多线程支持,使得并发编程变得相对简单。
// 场景:GUI应用中避免界面卡顿publicclassUIExample{publicvoidloadData(){newThread(()->{// 耗时操作在后台线程执行List<Data>data=fetchDataFromDatabase();// 更新UISwingUtilities.invokeLater(()->updateUI(data));}).start();}}// 场景:大数据处理、图像处理publicclassDataProcessor{publicvoidprocessLargeDataset(List<Data>dataset){intcores=Runtime.getRuntime().availableProcessors();ExecutorServiceexecutor=Executors.newFixedThreadPool(cores);for(Datadata:dataset){executor.submit(()->processData(data));}executor.shutdown();}}// 场景:发送邮件、消息通知publicclassNotificationService{privateExecutorServiceexecutor=Executors.newCachedThreadPool();publicvoidsendNotification(Useruser,Stringmessage){executor.submit(()->{emailService.send(user.getEmail(),message);smsService.send(user.getPhone(),message);});}}// 场景:Web服务器、API网关publicclassWebServer{publicvoidhandleRequest(Requestrequest){// 每个请求在独立线程中处理threadPool.execute(()->{Responseresponse=processRequest(request);sendResponse(response);});}}// 场景:数据同步、缓存刷新publicclassScheduledTaskExample{privateScheduledExecutorServicescheduler=Executors.newScheduledThreadPool(1);publicvoidstartScheduledTask(){// 每5秒执行一次scheduler.scheduleAtFixedRate(()->refreshCache(),0,5,TimeUnit.SECONDS);}}publicclassMyThreadextendsThread{@Overridepublicvoidrun(){System.out.println("线程执行: "+Thread.currentThread().getName());}publicstaticvoidmain(String[]args){MyThreadthread=newMyThread();thread.start();}}优点:实现简单
缺点:Java单继承限制,无法继承其他类
publicclassMyRunnableimplementsRunnable{@Overridepublicvoidrun(){System.out.println("线程执行: "+Thread.currentThread().getName());}publicstaticvoidmain(String[]args){Threadthread=newThread(newMyRunnable());thread.start();// Lambda表达式简化newThread(()->{System.out.println("Lambda方式");}).start();}}优点:避免单继承限制,代码解耦
缺点:无法直接获取返回值
publicclassMyCallableimplementsCallable<Integer>{@OverridepublicIntegercall()throwsException{intsum=0;for(inti=1;i<=100;i++){sum+=i;}returnsum;}publicstaticvoidmain(String[]args)throwsException{FutureTask<Integer>task=newFutureTask<>(newMyCallable());newThread(task).start();// 获取返回值(会阻塞)Integerresult=task.get();System.out.println("计算结果: "+result);}}优点:可以获取返回值,可以抛出异常
缺点:代码相对复杂
publicclassThreadPoolExample{publicstaticvoidmain(String[]args){// 1. 固定大小线程池ExecutorServicefixedPool=Executors.newFixedThreadPool(5);// 2. 缓存线程池ExecutorServicecachedPool=Executors.newCachedThreadPool();// 3. 单线程池ExecutorServicesinglePool=Executors.newSingleThreadExecutor();// 4. 定时线程池ScheduledExecutorServicescheduledPool=Executors.newScheduledThreadPool(3);// 5. 自定义线程池(推荐)ThreadPoolExecutorcustomPool=newThreadPoolExecutor(5,// 核心线程数10,// 最大线程数60L,// 空闲线程存活时间TimeUnit.SECONDS,// 时间单位newLinkedBlockingQueue<>(100),// 任务队列newThreadPoolExecutor.CallerRunsPolicy()// 拒绝策略);// 提交任务for(inti=0;i<10;i++){inttaskId=i;customPool.submit(()->{System.out.println("执行任务 "+taskId);});}// 关闭线程池customPool.shutdown();}}publicclassSynchronizedExample{privateintcount=0;// 同步方法publicsynchronizedvoidincrement(){count++;}// 同步代码块publicvoiddecrement(){synchronized(this){count--;}}}publicclassLockExample{privatefinalReentrantLocklock=newReentrantLock();privateintcount=0;publicvoidincrement(){lock.lock();try{count++;}finally{lock.unlock();}}}publicclassAtomicExample{privateAtomicIntegercount=newAtomicInteger(0);publicvoidincrement(){count.incrementAndGet();}publicintgetCount(){returncount.get();}}publicclassVolatileExample{privatevolatilebooleanrunning=true;publicvoidstop(){running=false;}publicvoidrun(){while(running){// 执行任务}}}publicclassProducerConsumerExample{privatestaticfinalintCAPACITY=10;privateBlockingQueue<Integer>queue=newLinkedBlockingQueue<>(CAPACITY);// 生产者classProducerimplementsRunnable{@Overridepublicvoidrun(){try{for(inti=0;i<20;i++){queue.put(i);System.out.println("生产: "+i);Thread.sleep(100);}}catch(InterruptedExceptione){Thread.currentThread().interrupt();}}}// 消费者classConsumerimplementsRunnable{@Overridepublicvoidrun(){try{while(true){Integeritem=queue.take();System.out.println("消费: "+item);Thread.sleep(200);}}catch(InterruptedExceptione){Thread.currentThread().interrupt();}}}publicstaticvoidmain(String[]args){ProducerConsumerExampleexample=newProducerConsumerExample();newThread(example.newProducer()).start();newThread(example.newConsumer()).start();}}// ❌ 不推荐newThread(()->doSomething()).start();// ✅ 推荐ExecutorServiceexecutor=Executors.newFixedThreadPool(10);executor.submit(()->doSomething());// CPU密集型:线程数 = CPU核心数 + 1intcpuIntensive=Runtime.getRuntime().availableProcessors()+1;// IO密集型:线程数 = CPU核心数 * 2intioIntensive=Runtime.getRuntime().availableProcessors()*2;// 按固定顺序获取锁publicvoidtransfer(Accountfrom,Accountto,intamount){Accountfirst=from.getId()<to.getId()?from:to;Accountsecond=from.getId()<to.getId()?to:from;synchronized(first){synchronized(second){from.debit(amount);to.credit(amount);}}}executor.shutdown();// 不再接受新任务try{if(!executor.awaitTermination(60,TimeUnit.SECONDS)){executor.shutdownNow();// 强制关闭}}catch(InterruptedExceptione){executor.shutdownNow();}// 线程安全的集合ConcurrentHashMap<String,Integer>map=newConcurrentHashMap<>();CopyOnWriteArrayList<String>list=newCopyOnWriteArrayList<>();BlockingQueue<Task>queue=newLinkedBlockingQueue<>();start():启动新线程,JVM调用run()方法run():普通方法调用,在当前线程执行sleep():Thread类方法,不释放锁wait():Object类方法,释放锁,需要notify()唤醒// ✅ 推荐:使用标志位privatevolatilebooleanrunning=true;publicvoidstop(){running=false;}// ❌ 不推荐:使用stop()方法(已废弃)Java多线程是提升程序性能和响应速度的重要手段。选择合适的实现方式和同步机制,遵循最佳实践,可以编写出高效、安全的并发程序。
关键要点:
希望这篇文章能帮助你更好地理解和使用Java多线程!