java實現(xiàn)多線程交替打印兩個數(shù)
本文實例為大家分享了java實現(xiàn)多線程交替打印兩個數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
方法1、使用wait和notify
package com.thread;
public class T01 {
public static void main(String[] args) {
char[] char1 = "AAAAAA".toCharArray();
char[] char2 = "BBBBBB".toCharArray();
Object object = new Object();
Thread thread1 = new Thread(() -> {
synchronized(object){//使用notify和wait時,必須要選獲取到鎖
for (int i = 0; i < char1.length; i++) {
try {
System.out.print(char1[i]);
object.notify();
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
object.notify();//必須加上,否則程序無法結(jié)束,兩個線程總有一個最后是wait狀態(tài),所以此處必須加
}
},"t1");
Thread thread2 = new Thread( () -> {
synchronized(object){
for (int i = 0; i < char2.length; i++) {
try {
System.out.print(char2[i]);
object.notify();
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
object.notify();
}
},"t2");
thread1.start();
thread2.start();
}
}
方法2、使用LockSupport方法
package com.thread;
import java.util.concurrent.locks.LockSupport;
public class T02 {
static Thread thread1 ;
static Thread thread2 ;
public static void main(String[] args) {
char[] char1 = "AAAAAA".toCharArray();
char[] char2 = "BBBBBB".toCharArray();
thread1 = new Thread(() -> {
for (int i = 0; i < char1.length; i++) {
System.out.print(char1[i]);
LockSupport.unpark(thread2);
LockSupport.park();
}
},"t1");
thread2 = new Thread(() -> {
for (int i = 0; i < char2.length; i++) {
LockSupport.park();
System.out.print(char2[i]);
LockSupport.unpark(thread1);
}
},"t2");
thread1.start();
thread2.start();
}
}
方法3、使用CAS自旋鎖
package com.thread;
public class T03 {
enum ReadEnum{
T1,
T2;
}
static volatile ReadEnum r = ReadEnum.T1;
public static void main(String[] args) {
char[] char1 = "AAAAAA".toCharArray();
char[] char2 = "BBBBBB".toCharArray();
Thread thread1 = new Thread(() ->{
for (int i = 0; i < char1.length; i++) {
while (r != ReadEnum.T1) {
}
System.out.print(char1[i]);
r = ReadEnum.T2;
}
},"t1");
Thread thread2 = new Thread(() ->{
for (int i = 0; i < char2.length; i++) {
while (r != ReadEnum.T2) {
}
System.out.print(char2[i]);
r = ReadEnum.T1;
}
},"t2");
thread1.start();
thread2.start();
}
}
方法4、使用Condition方法
package com.thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class T04 {
public static void main(String[] args) {
char[] char1 = "AAAAAA".toCharArray();
char[] char2 = "BBBBBB".toCharArray();
ReentrantLock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Thread thread1 = new Thread(() ->{
try {
lock.lock();
for (int i = 0; i < char1.length; i++) {
System.out.print(char1[i]);
condition2.signal();//喚醒線程2執(zhí)行
condition1.await();//線程1等待
}
condition2.signal();
}catch (Exception e) {
e.printStackTrace();
}finally{
lock.unlock();
}
},"t1");
Thread thread2 = new Thread(() ->{
try {
lock.lock();
for (int i = 0; i < char2.length; i++) {
System.out.print(char2[i]);
condition1.signal();
condition2.await();
}
condition1.signal();
} catch (Exception e) {
e.printStackTrace();
}finally{
lock.unlock();
}
},"t2");
thread1.start();
thread2.start();
}
}
Condition與notify相比的好處是,Condition可以指定需要喚醒的線程,而notify是無法指定的,只能隨機喚醒一個或者全喚醒(notifyAll)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用redis生成訂單號的實現(xiàn)示例
在電商系統(tǒng)中,生成唯一訂單號是常見需求,本文介紹如何利用SpringBoot和Redis實現(xiàn)訂單號的生成,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-09-09
基于String和List<String>間的相互轉(zhuǎn)換方式
這篇文章主要介紹了基于String和List間的相互轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實用
這篇文章主要介紹了詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
淺談SpringSecurity注解與AOP切面執(zhí)行順序
這篇文章主要介紹了淺談SpringSecurity注解與AOP切面執(zhí)行順序,引入Spring Security后,在Controller的方法中會出現(xiàn)Spring Security的方法注解與AOP同時存在的問題,這是就會設(shè)計順序問題,需要的朋友可以參考下2023-10-10

