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

相关推荐
2601_962885726 小时前
如何用 Python 计算 TRIX 三重指数平滑均线指标?
开发语言·python
还卿一钵无情泪7 小时前
Unsloth 微调 构建自己的大模型 没有GPU也能微调
linux·开发语言·人工智能·python·大模型·nlp·unsloth
小π军8 小时前
操作系统面试题
java·linux·服务器
倔强的石头1068 小时前
应用账号最小权限实践:读写账号、报表账号、运维账号分层
java·运维·开发语言
传人once9 小时前
页面中心圆圈放大效果如何写
开发语言·javascript·ecmascript
NeilYuen9 小时前
【C++】STL源码仿写(二):vector
开发语言·c++
Shaoshing9 小时前
ThreadLocal
java·jvm·threadlocal
写后端的胖头鱼9 小时前
一文详解CAS
java·jvm·spring·cas·乐观锁·自旋
鱼宵9 小时前
Spring AI 提示词模板:{变量} 参数化 + few-shot,一条提示词反复用
java·人工智能·spring·few-shot·提示词工程·springai