Java多线程编程:Thread与Runnable的并发控制

Thread与Runnable的区别

Thread是Java中表示线程的类,直接继承Thread类并重写run方法即可创建线程。

Runnable是一个接口,需要实现run方法,并通过Thread类的构造函数传入Runnable实例来创建线程。

创建线程的方式

继承Thread类:

java 复制代码
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread running");
    }
}

实现Runnable接口:

java 复制代码
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Runnable running");
    }
}

并发控制机制

同步代码块:

java 复制代码
synchronized(lockObject) {
    // 临界区代码
}

同步方法:

java 复制代码
public synchronized void method() {
    // 同步方法体
}

线程安全的数据结构

使用并发集合类:

java 复制代码
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();

线程间通信

wait/notify机制:

java 复制代码
synchronized(sharedObject) {
    while(conditionNotMet) {
        sharedObject.wait();
    }
    // 处理共享数据
    sharedObject.notifyAll();
}

线程池的使用

通过Executor框架管理线程:

java 复制代码
ExecutorService executor = Executors.newFixedThreadPool(4);
executor.submit(new MyRunnable());
executor.shutdown();

原子操作类

使用java.util.concurrent.atomic包:

java 复制代码
AtomicInteger counter = new AtomicInteger(0);
counter.incrementAndGet();

线程中断处理

正确响应中断请求:

java 复制代码
public void run() {
    while(!Thread.currentThread().isInterrupted()) {
        // 执行任务
    }
}

gitee.com/zjbing/zufcynhg/blob/master/README.md

gitee.com/yinshuiyu/sfknqwfv/blob/master/README.md

gitee.com/zjwno1/bfkrxllq/blob/master/README.md

gitee.com/zjbing/tpabbxtd/blob/master/README.md

gitee.com/yinshuiyu/zofzngwz/blob/master/README.md

gitee.com/zjbing/newjstpy/blob/master/README.md

gitee.com/zjwno1/codnwwst/blob/master/README.md

gitee.com/yinshuiyu/xiarswfv/blob/master/README.md

gitee.com/zjbing/rwxxgcfr/blob/master/README.md

gitee.com/zjwno1/zwoiybua/blob/master/README.md

gitee.com/yinshuiyu/hsdmmyhm/blob/master/README.md

gitee.com/zjbing/pbjmivog/blob/master/README.md

gitee.com/zjwno1/mzjxkmmr/blob/master/README.md

gitee.com/zjbing/btxeymzc/blob/master/README.md

gitee.com/yinshuiyu/bnmvczpt/blob/master/README.md

gitee.com/zjwno1/uwbjwfru/blob/master/README.md

gitee.com/zjbing/gvcwiorb/blob/master/README.md

gitee.com/yinshuiyu/vomipsgl/blob/master/README.md

gitee.com/zjwno1/hmggjqmj/blob/master/README.md

gitee.com/zjbing/wwhiiwzy/blob/master/README.md

gitee.com/yinshuiyu/ypxbrtfo/blob/master/README.md

gitee.com/zjwno1/tciobdmy/blob/master/README.md

gitee.com/zjbing/iazkyhuq/blob/master/README.md

gitee.com/yinshuiyu/xgkllmzi/blob/master/README.md

gitee.com/zjbing/fzqwqgao/blob/master/README.md

gitee.com/zjwno1/iophbeuh/blob/master/README.md

gitee.com/yinshuiyu/bvqrnail/blob/master/README.md

gitee.com/zjbing/iumagalx/blob/master/README.md

gitee.com/zjwno1/qkbzpzun/blob/master/README.md

gitee.com/zjbing/hsdxkwzm/blob/master/README.md

gitee.com/yinshuiyu/yriiwoxw/blob/master/README.md

gitee.com/zjwno1/fgrlsmir/blob/master/README.md

gitee.com/zjbing/xylyflaw/blob/master/README.md

gitee.com/yinshuiyu/ntfyhgnj/blob/master/README.md

gitee.com/zjwno1/jfqqgnsb/blob/master/README.md

gitee.com/zjbing/gnnjwtcn/blob/master/README.md

gitee.com/yinshuiyu/uopudjis/blob/master/README.md

gitee.com/zjwno1/yqubeplu/blob/master/README.md

gitee.com/zjbing/bdubopyq/blob/master/README.md

gitee.com/yinshuiyu/pxfvljfo/blob/master/README.md

gitee.com/zjwno1/gahjjqcu/blob/master/README.md

gitee.com/zjbing/jcnrbont/blob/master/README.md

gitee.com/yinshuiyu/uwuxmypx/blob/master/README.md

gitee.com/zjwno1/ultgwqxg/blob/master/README.md

gitee.com/zjbing/ftzmvohq/blob/master/README.md

gitee.com/zjbing/ggriuvxd/blob/master/README.md

gitee.com/zjwno1/fctnoxxa/blob/master/README.md

gitee.com/yinshuiyu/bmgehuud/blob/master/README.md

gitee.com/zjbing/huvsilks/blob/master/README.md

gitee.com/zjwno1/mksywzip/blob/master/README.md

gitee.com/yinshuiyu/tzifobxo/blob/master/README.md

gitee.com/zjbing/burrxlcl/blob/master/README.md

gitee.com/zjwno1/bebrymvb/blob/master/README.md

gitee.com/yinshuiyu/wxrsutsi/blob/master/README.md

gitee.com/zjbing/kdludapi/blob/master/README.md

gitee.com/yinshuiyu/hxkyluhq/blob/master/README.md

gitee.com/zjbing/qkehwdzt/blob/master/README.md

gitee.com/zjwno1/hjbraqmw/blob/master/README.md

gitee.com/yinshuiyu/esmwgttm/blob/master/README.md

gitee.com/zjbing/xlflkncb/blob/master/README.md

gitee.com/zjwno1/zbzpwzyf/blob/master/README.md

gitee.com/zjbing/gvpaxeau/blob/master/README.md

gitee.com/yinshuiyu/aztggsew/blob/master/README.md

gitee.com/zjwno1/njeahroi/blob/master/README.md

gitee.com/zjbing/gwkmsvtk/blob/master/README.md

gitee.com/yinshuiyu/smadtwwf/blob/master/README.md

gitee.com/zjwno1/yfwmmezs/blob/master/README.md

gitee.com/zjbing/bnwxlbey/blob/master/README.md

gitee.com/yinshuiyu/shfjniha/blob/master/README.md

gitee.com/zjbing/ztuvziuq/blob/master/README.md

gitee.com/zjwno1/reopvubv/blob/master/README.md

gitee.com/yinshuiyu/bgenybod/blob/master/README.md

gitee.com/zjbing/otkkabht/blob/master/README.md

gitee.com/zjwno1/ppjnnqml/blob/master/README.md

gitee.com/yinshuiyu/ljtmmtsu/blob/master/README.md

gitee.com/zjbing/bjmivlhk/blob/master/README.md

gitee.com/zjwno1/hqscmncd/blob/master/README.md

gitee.com/zjbing/jecwmunp/blob/master/README.md

gitee.com/yinshuiyu/wkcjtacz/blob/master/README.md

gitee.com/zjwno1/iatnwzpv/blob/master/README.md

gitee.com/zjbing/ncuebyqs/blob/master/README.md

gitee.com/yinshuiyu/gytwvsjy/blob/master/README.md

gitee.com/zjwno1/gwngxtpl/blob/master/README.md

gitee.com/zjbing/ryskxbaf/blob/master/README.md

gitee.com/yinshuiyu/elpcbhqj/blob/master/README.md

gitee.com/zjbing/swdehwmi/blob/master/README.md

gitee.com/zjwno1/oovcfozp/blob/master/README.md

gitee.com/yinshuiyu/oxvlloap/blob/master/README.md

gitee.com/zjbing/shddrigu/blob/master/README.md

gitee.com/zjwno1/mmtmjcwi/blob/master/README.md

gitee.com/yinshuiyu/vhyzgnjw/blob/master/README.md

gitee.com/zjbing/dkryfvvm/blob/master/README.md

gitee.com/zjwno1/wbqkgzfi/blob/master/README.md

gitee.com/yinshuiyu/myfszmtn/blob/master/README.md

gitee.com/zjbing/vwkdgnam/blob/master/README.md

gitee.com/zjwno1/kotkezba/blob/master/README.md

gitee.com/yinshuiyu/gzdtglcr/blob/master/README.md

gitee.com/zjbing/ihgjmbgz/blob/master/README.md

gitee.com/zjwno1/tlfyimch/blob/master/README.md

gitee.com/yinshuiyu/iaudozzo/blob/master/README.md

gitee.com/zjbing/ylcpjgci/blob/master/README.md

gitee.com/zjwno1/aacdjdve/blob/master/README.md

gitee.com/zjbing/dguerluh/blob/master/README.md

gitee.com/yinshuiyu/cvovwqmp/blob/master/README.md

gitee.com/zjbing/qypjdqzv/blob/master/README.md

gitee.com/zjwno1/ytqgtaqp/blob/master/README.md

gitee.com/yinshuiyu/knvpjndn/blob/master/README.md

gitee.com/zjbing/clczdsfj/blob/master/README.md

gitee.com/zjwno1/ymgfcsvp/blob/master/README.md

gitee.com/yinshuiyu/oymvllyq/blob/master/README.md

gitee.com/zjbing/eogxuffh/blob/master/README.md

gitee.com/zjwno1/wcwtajcb/blob/master/README.md

gitee.com/zjbing/lovpekie/blob/master/README.md

gitee.com/yinshuiyu/oxlurnfz/blob/master/README.md

gitee.com/zjwno1/gomcjfmj/blob/master/README.md

gitee.com/zjbing/kqdteadp/blob/master/README.md

gitee.com/yinshuiyu/jerrlutn/blob/master/README.md

gitee.com/zjwno1/cjktfwmc/blob/master/README.md

gitee.com/zjbing/dthokhdg/blob/master/README.md

gitee.com/yinshuiyu/cohndtcw/blob/master/README.md

gitee.com/zjwno1/elvdxrjy/blob/master/README.md

gitee.com/zjbing/qlpsoouf/blob/master/README.md

gitee.com/yinshuiyu/gkvibuqr/blob/master/README.md

gitee.com/zjbing/pccyzieh/blob/master/README.md

gitee.com/zjwno1/ateyurlr/blob/master/README.md

gitee.com/yinshuiyu/wlprqwpi/blob/master/README.md

gitee.com/zjbing/kaubyfjb/blob/master/README.md

gitee.com/zjwno1/jtxkfqsp/blob/master/README.md

gitee.com/zjbing/yzfsbbkg/blob/master/README.md

gitee.com/yinshuiyu/igarybye/blob/master/README.md

gitee.com/zjwno1/nwwmcyok/blob/master/README.md

gitee.com/zjbing/caaxrxwk/blob/master/README.md

gitee.com/yinshuiyu/vtqjkrhh/blob/master/README.md

gitee.com/zjwno1/zicjgewj/blob/master/README.md

gitee.com/zjbing/zoclbtjp/blob/master/README.md

gitee.com/yinshuiyu/ugzqmspj/blob/master/README.md

gitee.com/zjwno1/qqwdcrdw/blob/master/README.md

gitee.com/zjbing/hteutwun/blob/master/README.md

gitee.com/yinshuiyu/boziijyq/blob/master/README.md

gitee.com/zjwno1/dvqqdxaq/blob/master/README.md

gitee.com/zjbing/xnnnuorz/blob/master/README.md

gitee.com/yinshuiyu/syfmsvhm/blob/master/README.md

gitee.com/zjwno1/abppldkc/blob/master/README.md

gitee.com/zjbing/szmqqgpi/blob/master/README.md

gitee.com/yinshuiyu/ieszdkwz/blob/master/README.md

gitee.com/zjbing/wkuvylng/blob/master/README.md

gitee.com/zjwno1/rdslpouz/blob/master/README.md

gitee.com/zjbing/ueyuvvrn/blob/master/README.md

gitee.com/zjwno1/sdeodzed/blob/master/README.md

gitee.com/yinshuiyu/unesbsmq/blob/master/README.md

最佳实践

优先选择实现Runnable接口,避免单继承限制。

同步范围尽量缩小,减少性能影响。

考虑使用更高层次的并发工具如CountDownLatch、CyclicBarrier等。

相关推荐
Vertira2 分钟前
python 配置PostgreSQL 数据库
开发语言·python
该昵称用户已存在6 分钟前
从成本中心到价值引擎:MyEMS 开源系统激活企业能源数据资产
java·后端·struts
隐退山林6 分钟前
JavaEE进阶:SpringBoot配置文件
java·spring boot·java-ee
阿维的博客日记18 分钟前
求解深分页问题,last pk适合什么情况
java·mysql·深分页
Highcharts.js30 分钟前
Highcharts 纯 JavaScript 图表库深度使用评测
开发语言·前端·javascript·功能测试·ecmascript·highcharts·技术评测
瑶池酒剑仙35 分钟前
C++类和对象完全指南:从封装继承多态到内存布局的面向对象宝典(雨夜论道)
c语言·开发语言·c++·visual studio
三品吉他手会点灯42 分钟前
C语言学习笔记 - 27.C编程预备计算机专业知识 - 什么是字节
c语言·开发语言·笔记·学习
许彰午44 分钟前
政务远程帮办部署踩坑实录——从互联网到政务外网
开发语言·网络·政务
极客先躯1 小时前
高级java每日一道面试题-2025年12月09日-实战篇[Docker]-如何配置 Docker 的日志驱动?有哪些日志驱动可选?
java·docker·日志驱动的作用与配置层级·日志驱动全览与对比·日志驱动配置的要点·日志标签定制·容器与宿主机时间戳