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

相关推荐
逆境不可逃2 分钟前
Pi Agent 学习笔记:扩展如何介入工具调用
java
用户0942485680327 分钟前
第12章:JDK 诊断工具箱——jps / jstat / jmap / jcmd / jhsdb
java·jvm
aramae31 分钟前
Python 使用库:标准库与第三方库实战
服务器·开发语言·windows·python
景熙552344 分钟前
14.Java 集合框架从入门到源码万字详解(含 ArrayList/LinkedList/HashMap 源码、泛型通配符、红黑树)
java·开发语言·数据结构·算法
五仁火烧1 小时前
大模型开发: MCP
java·大模型·mcp
yuzhiboyouye1 小时前
XML写接口适用场景举例
java·服务器·前端
今天的接口写完了吗?1 小时前
Java 使用 OkHttp 调用外部接口
java
yuzhiboyouye1 小时前
零sql和xml写接口,的区别是什么,过程是什么,举例一下下,用mybatis-plus+ spring-boot
java·服务器·前端
小王师傅662 小时前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
nvvas2 小时前
Spring AI 2.0正式GA:Token砍掉64%,Java的AI反击战打响了
java·人工智能