多线程交叉打印数字(1-75)
发布日期:2022-04-11 08:52:50 浏览次数:9 分类:技术文章

本文共 12068 字,大约阅读时间需要 40 分钟。

设置同步标志位

package JTanG.ChinaEmp;import java.util.concurrent.locks.Lock;public class Test2 {
public volatile static int flag = 0; public volatile static int i = 0; public static void main(String[] args) {
Thread thread = new Thread(() -> {
while (i < 75){
if(flag == 0){
System.out.println("thread-1:"+i++); flag = 1; } } }); Thread thread1 = new Thread(() -> {
while (i < 75){
if(flag == 1){
System.out.println("thread-2:"+i++); flag = 0; } } }); thread.start(); thread1.start(); }}

在这里插入图片描述

三个线程,每个线程输出五个数字

package JTanG.ChinaEmp;import java.util.concurrent.locks.Lock;public class Test2 {
public volatile static int flag = 1; public volatile static int i = 0; public static void main(String[] args) {
Thread thread = new Thread(() -> {
while (i < 75){
if(flag == 1){
for (int j = 0; j < 5; j++) {
System.out.println("thread-1:"+i++); } flag = 2; } } }); Thread thread1 = new Thread(() -> {
while (i < 75){
if(flag == 2){
for (int j = 0; j < 5; j++) {
System.out.println("thread-2:"+i++); } flag = 3; } } }); Thread thread2 = new Thread(() -> {
while (i < 75){
if(flag == 3){
for (int j = 0; j < 5; j++) {
System.out.println("thread-3:"+i++); } flag = 1; } } }); thread.start(); thread1.start(); thread2.start(); }}
"D:\IntelliJ IDEA 2019.3.3\jbr\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=6426:D:\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\Java开发\Project_ArrayList\out\production\Project_ArrayList;C:\Users\唐昊\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\唐昊\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar JTanG.ChinaEmp.Test2thread-1:0thread-1:1thread-1:2thread-1:3thread-1:4thread-2:5thread-2:6thread-2:7thread-2:8thread-2:9thread-3:10thread-3:11thread-3:12thread-3:13thread-3:14thread-1:15thread-1:16thread-1:17thread-1:18thread-1:19thread-2:20thread-2:21thread-2:22thread-2:23thread-2:24thread-3:25thread-3:26thread-3:27thread-3:28thread-3:29thread-1:30thread-1:31thread-1:32thread-1:33thread-1:34thread-2:35thread-2:36thread-2:37thread-2:38thread-2:39thread-3:40thread-3:41thread-3:42thread-3:43thread-3:44thread-1:45thread-1:46thread-1:47thread-1:48thread-1:49thread-2:50thread-2:51thread-2:52thread-2:53thread-2:54thread-3:55thread-3:56thread-3:57thread-3:58thread-3:59thread-1:60thread-1:61thread-1:62thread-1:63thread-1:64thread-2:65thread-2:66thread-2:67thread-2:68thread-2:69thread-3:70thread-3:71thread-3:72thread-3:73thread-3:74Process finished with exit code 0

Lock形式

package JTanG.ChinaEmp;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class Test3Plus {
public volatile static int flag = 1; public volatile static int count = 0; public static void main(String[] args) {
Lock threadLock = new ReentrantLock(); Thread thread1 = new Thread(() -> {
while (count < 75) {
threadLock.lock(); try {
if (flag == 1) {
for (int i = 0; i < 5; i++) {
System.out.println("aThread--1 --> " + (count++)); } flag = 2; } } catch (Exception e) {
} finally {
threadLock.unlock(); } } }); Thread thread2 = new Thread(() -> {
while (count < 75) {
threadLock.lock(); try {
if (flag == 2) {
for (int i = 0; i < 5; i++) {
System.out.println("aThread --2--> " + (count++)); } flag = 3; } } catch (Exception e) {
} finally {
threadLock.unlock(); } } }); Thread thread3 = new Thread(() -> {
while (count < 75) {
threadLock.lock(); try {
if (flag == 3) {
for (int i = 0; i < 5; i++) {
System.out.println("aThread --3--> " + (count++)); } flag = 1; } } catch (Exception e) {
} finally {
threadLock.unlock(); } } }); thread1.start(); thread2.start(); thread3.start(); }}
"D:\IntelliJ IDEA 2019.3.3\jbr\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=8703:D:\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\Java开发\Project_ArrayList\out\production\Project_ArrayList;C:\Users\唐昊\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\唐昊\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar JTanG.ChinaEmp.Test3PlusaThread--1 --> 0aThread--1 --> 1aThread--1 --> 2aThread--1 --> 3aThread--1 --> 4aThread --2--> 5aThread --2--> 6aThread --2--> 7aThread --2--> 8aThread --2--> 9aThread  --3--> 10aThread  --3--> 11aThread  --3--> 12aThread  --3--> 13aThread  --3--> 14aThread--1 --> 15aThread--1 --> 16aThread--1 --> 17aThread--1 --> 18aThread--1 --> 19aThread --2--> 20aThread --2--> 21aThread --2--> 22aThread --2--> 23aThread --2--> 24aThread  --3--> 25aThread  --3--> 26aThread  --3--> 27aThread  --3--> 28aThread  --3--> 29aThread--1 --> 30aThread--1 --> 31aThread--1 --> 32aThread--1 --> 33aThread--1 --> 34aThread --2--> 35aThread --2--> 36aThread --2--> 37aThread --2--> 38aThread --2--> 39aThread  --3--> 40aThread  --3--> 41aThread  --3--> 42aThread  --3--> 43aThread  --3--> 44aThread--1 --> 45aThread--1 --> 46aThread--1 --> 47aThread--1 --> 48aThread--1 --> 49aThread --2--> 50aThread --2--> 51aThread --2--> 52aThread --2--> 53aThread --2--> 54aThread  --3--> 55aThread  --3--> 56aThread  --3--> 57aThread  --3--> 58aThread  --3--> 59aThread--1 --> 60aThread--1 --> 61aThread--1 --> 62aThread--1 --> 63aThread--1 --> 64aThread --2--> 65aThread --2--> 66aThread --2--> 67aThread --2--> 68aThread --2--> 69aThread  --3--> 70aThread  --3--> 71aThread  --3--> 72aThread  --3--> 73aThread  --3--> 74aThread--1 --> 75aThread--1 --> 76aThread--1 --> 77aThread--1 --> 78aThread--1 --> 79aThread --2--> 80aThread --2--> 81aThread --2--> 82aThread --2--> 83aThread --2--> 84Process finished with exit code 0

synchronized用法

package JTanG.ChinaEmp;import JTanG.Leetcode.Thre;import java.util.concurrent.locks.ReentrantLock;public class Test4 {
public static volatile int num = 0; public static void main(String[] args) throws InterruptedException {
Object people = new Object(); Thread thread1 = new Thread(() -> {
while (num < 75) {
synchronized (people) {
for (int i = 0; i < 5; i++) {
System.out.println("thread-1 --->" + num++); } people.notifyAll(); try {
people.wait(); } catch (InterruptedException e) {
e.printStackTrace(); } } } }); Thread thread2 = new Thread(() -> {
while (num < 75) {
synchronized (people) {
for (int i = 0; i < 5; i++) {
System.out.println("thread-2 --->" + num++); } people.notifyAll(); try {
people.wait(); } catch (InterruptedException e) {
e.printStackTrace(); } } } }); Thread thread3 = new Thread(() -> {
while (num < 75) {
synchronized (people) {
for (int i = 0; i < 5; i++) {
System.out.println("thread-3 --->" + num++); } people.notifyAll(); try {
people.wait(); } catch (InterruptedException e) {
e.printStackTrace(); } } } }); thread1.start(); thread2.start(); thread3.start(); }}
"D:\IntelliJ IDEA 2019.3.3\jbr\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=9128:D:\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\Java开发\Project_ArrayList\out\production\Project_ArrayList;C:\Users\唐昊\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\唐昊\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar JTanG.ChinaEmp.Test4thread-1 --->0thread-1 --->1thread-1 --->2thread-1 --->3thread-1 --->4thread-3   --->5thread-3   --->6thread-3   --->7thread-3   --->8thread-3   --->9thread-2  --->10thread-2  --->11thread-2  --->12thread-2  --->13thread-2  --->14thread-3   --->15thread-3   --->16thread-3   --->17thread-3   --->18thread-3   --->19thread-1 --->20thread-1 --->21thread-1 --->22thread-1 --->23thread-1 --->24thread-2  --->25thread-2  --->26thread-2  --->27thread-2  --->28thread-2  --->29thread-3   --->30thread-3   --->31thread-3   --->32thread-3   --->33thread-3   --->34thread-1 --->35thread-1 --->36thread-1 --->37thread-1 --->38thread-1 --->39thread-2  --->40thread-2  --->41thread-2  --->42thread-2  --->43thread-2  --->44thread-3   --->45thread-3   --->46thread-3   --->47thread-3   --->48thread-3   --->49thread-1 --->50thread-1 --->51thread-1 --->52thread-1 --->53thread-1 --->54thread-2  --->55thread-2  --->56thread-2  --->57thread-2  --->58thread-2  --->59thread-3   --->60thread-3   --->61thread-3   --->62thread-3   --->63thread-3   --->64thread-1 --->65thread-1 --->66thread-1 --->67thread-1 --->68thread-1 --->69thread-2  --->70thread-2  --->71thread-2  --->72thread-2  --->73thread-2  --->74

AtomicInteger 形式

package JTanG.ChinaEmp;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;public class TestAuto {
public static AtomicInteger num = new AtomicInteger(0); public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true){
int i = num.get(); if (i < 75 ){
if(num.compareAndSet(i,i+1)){
System.out.println("thread-1 ->"+num.get()); } }else {
break; } } }); Thread thread1 = new Thread(() -> {
while (true){
int i = num.get(); if (i < 75 ){
if(num.compareAndSet(i,i+1)){
System.out.println("thread-2 -->"+num.get()); } }else {
break; } } }); thread.start(); thread.setPriority(10); thread1.setPriority(1); thread1.start(); }}

在这里插入图片描述

转载地址:https://tanghao.blog.csdn.net/article/details/123428978 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:多线程交替输出问题3种常用解法(多线程经典面试题)
下一篇:多线程五 synchronized实现原理及优化

发表评论

最新留言

很好
[***.229.124.182]2024年04月22日 03时07分29秒