[Java] 多线程
例1:单线程
Punishment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Punishment { private int leftCopyCount; private String wordToCopy; public Punishment( int i, String s) { this .leftCopyCount = i; this .wordToCopy = s; } public int getLeftCopyCount() { return leftCopyCount; } public void setLeftCopyCount( int leftCopyCount) { this .leftCopyCount = leftCopyCount; } public String getWordToCopy() { return wordToCopy; } public void setWordToCopy(String wordToCopy) { this .wordToCopy = wordToCopy; } } |
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class Student { private String name; private Punishment punishment; public Student(String name,Punishment punishment){ this .name = name; this .punishment = punishment; } public void copyWord(){ int count = 0 ; String threadName = Thread.currentThread().getName(); while ( true ){ if (punishment.getLeftCopyCount() > 0 ){ int leftCopyCount = punishment.getLeftCopyCount(); System.out.println(threadName+ "线程-" + name + "抄写" + punishment.getWordToCopy() + "。还要抄写" + --leftCopyCount + "次" ); punishment.setLeftCopyCount(leftCopyCount); count++; } else { break ; } } System.out.println(threadName + "线程-" + name + "一共抄写了" + count + "次!" ); } } |
StudentClient.java
1 2 3 4 5 6 7 | public class StudentClient { public static void main(String[] args) { Punishment punishment = new Punishment( 100 , "internationalization" ); Student student = new Student( "小明" ,punishment); student.copyWord(); } } |
main线程-小明抄写internationalization。还要抄写99次
...
main线程-小明抄写internationalization。还要抄写0次
main线程-小明一共抄写了100次!
例2:继承Thread实现独立线程
与例1不同处:
- 继承Thread类
- 调用Thread构造方法,设置threadName
- 重写run方法,调用copyWord完成任务
- 在main()中调用 start 方法
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class Student extends Thread{ private String name; private Punishment punishment; public Student(String name,Punishment punishment){ super (name); this .name = name; this .punishment = punishment; } public void copyWord(){ int count = 0 ; String threadName = Thread.currentThread().getName(); while ( true ){ if (punishment.getLeftCopyCount() > 0 ){ int leftCopyCount = punishment.getLeftCopyCount(); System.out.println(threadName+ "线程-" + name + "抄写" + punishment.getWordToCopy() + "。还要抄写" + --leftCopyCount + "次" ); punishment.setLeftCopyCount(leftCopyCount); count++; } else { break ; } } System.out.println(threadName + "线程-" + name + "一共抄写了" + count + "次!" ); } @Override public void run(){ copyWord(); } } |
StudentClient.java
1 2 3 4 5 6 7 8 | public class StudentClient { public static void main(String[] args) { Punishment punishment = new Punishment( 100 , "internationalization" ); Student student = new Student( "小明" ,punishment); student.start(); System.out.println( "Another thread will finish the punishment。 main thread is finished" ); } } |
Another thread will finish the punishment。 main thread is finished
小明线程-小明抄写internationalization。还要抄写99次
...
小明线程-小明抄写internationalization。还要抄写0次
小明线程-小明一共抄写了100次!
例2:继承Thread实现多线程
StudentClient.java
1 2 3 4 5 6 7 8 9 10 11 | public class StudentClient { public static void main(String[] args) { Punishment punishment = new Punishment( 100 , "internationalization" ); Student xiaoming = new Student( "小明" ,punishment); xiaoming.start(); Student xiaozhang = new Student( "小张" ,punishment); xiaozhang.start(); Student xiaozhao = new Student( "小赵" ,punishment); xiaozhao.start(); } } |
小明线程-小明一共抄写了100次!
小张线程-小张一共抄写了100次!
小赵线程-小赵一共抄写了100次!
例3:继承Runnable实现多线程
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class Student implements Runnable{ private String name; private Punishment punishment; public Student(String name,Punishment punishment){ this .name = name; this .punishment = punishment; } public void copyWord(){ int count = 0 ; String threadName = Thread.currentThread().getName(); while ( true ){ if (punishment.getLeftCopyCount() > 0 ){ int leftCopyCount = punishment.getLeftCopyCount(); System.out.println(threadName+ "线程-" + name + "抄写" + punishment.getWordToCopy() + "。还要抄写" + --leftCopyCount + "次" ); // --leftCopyCount; punishment.setLeftCopyCount(leftCopyCount); count++; } else { break ; } } System.out.println(threadName + "线程-" + name + "一共抄写了" + count + "次!" ); } @Override public void run(){ copyWord(); } } |
StudentClient.java
1 2 3 4 5 6 7 | public class StudentClient { public static void main(String[] args) { Punishment punishment = new Punishment( 100 , "internationalization" ); Thread xiaoming = new Thread( new Student( "小明" ,punishment)); xiaoming.start(); } } |
结果同例2
例4:多线程同时抄写
- 取得剩余次数后马上更新-1后的次数。看似是避免了读取和更新间的时间间隔
- 更新剩余次数前先判断自己的更新次数是否为最新,避免更新后次数反而变大的问题
- 有小概率两人甚至三人同时查看剩余次数,导致重复抄写
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | public class Student extends Thread{ private String name; private Punishment punishment; public Student(String name,Punishment punishment){ super (name); this .name = name; this .punishment = punishment; } public void copyWord(){ int count = 0 ; String threadName = Thread.currentThread().getName(); while ( true ){ if (punishment.getLeftCopyCount() > 0 ){ int leftCopyCount = punishment.getLeftCopyCount(); leftCopyCount--; if (leftCopyCount<punishment.getLeftCopyCount()){ punishment.setLeftCopyCount(leftCopyCount); } System.out.println(threadName+ "线程-" + name + "抄写" + punishment.getWordToCopy() + "。还要抄写" + leftCopyCount + "次" ); count++; } else { break ; } } System.out.println(threadName + "线程-" + name + "一共抄写了" + count + "次!" ); } @Override public void run(){ copyWord(); } } |
StudentClient.java
1 2 3 4 5 6 7 8 9 10 11 | public class StudentClient { public static void main(String[] args) { Punishment punishment = new Punishment( 100 , "internationalization" ); Student xiaoming = new Student( "小明" ,punishment); xiaoming.start(); Student xiaozhang = new Student( "小张" ,punishment); xiaozhang.start(); Student xiaozhao = new Student( "小赵" ,punishment); xiaozhao.start(); } } |
小赵线程-小赵一共抄写了34次!
小张线程-小张一共抄写了22次!
小明线程-小明一共抄写了44次!
例5:解决线程安全问题
参考
java多线程是并发还是并行
https://blog.csdn.net/gxl1989225/article/details/84912017
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)