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等。

相关推荐
南宫萧幕1 小时前
从YALMIP工具箱到车辆工况仿真:MATLAB控制策略开发的完整实践指南
开发语言·人工智能·matlab·simulink
泰迪智能科技011 小时前
图书教材推荐|Python网络爬虫技术(第2版)(微课版)
开发语言·爬虫·python
身如柳絮随风扬1 小时前
深入理解 Sentinel:服务雪崩、熔断原理、使用实践与规则持久化
java·微服务·sentinel
dgvri1 小时前
Spring Boot接收参数的19种方式
java·spring boot·后端
组合缺一1 小时前
SolonCode CLI 为什么选择 Java 技术栈?
java·开发语言
rOuN STAT1 小时前
Spring Boot 2.7.x 至 2.7.18 及更旧的版本,漏洞说明
java·spring boot·后端
阿巴斯甜2 小时前
BinaryOperator的使用:
java
计算机安禾2 小时前
【Linux从入门到精通】第10篇:软件包管理——Linux如何安装与卸载软件
java·linux·运维·服务器·编辑器
熊猫钓鱼>_>2 小时前
GenUI:从“文本对话”到“可操作界面”的范式转移
开发语言·人工智能·agent·sdk·vibecoding·assistant·genui