說,明:拿到一道面試題如下:
構造5個線程,從1-100每個線程依次輸出5個數字,
例如線程1輸出1-5,線程2輸出6-10,…,線程5輸出21-25,
然後線程1輸出26-30,依次類推到100截止。
一開始網上先自己查詢了嘿嘿想偷下懶,可是發現並滅有找到寫的合適的,故自己嘗試多次,優化,簡單重構了下,並實現了動態可擴展
,我覺得還不錯,分享下。
代碼如下(直接拿走,main 方法運行可見結果!):
package com.guigu.juc.communicate.lock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @Description: 多線程之間按順序調用,每個線程給定一個編號,實現xxx
*/
class ShareResource_test{
//CAS原子類測試--操作數
private AtomicInteger count = new AtomicInteger(0);
/** 每個線程給定一個編號--
* 第一次來默認設置爲 1 線程----
* 線程(1)完了後 通知線程(2),
* 線程(2)完了後通知線程(3),
* 線程(3)完了後通知線程(1) ....4.5....
* */
private int currentThreadNumber = 1;//默認第一個線程先能指向
private int threadNum ;
private int MAX_VALUE ;
private Lock lock = new ReentrantLock();/** 鎖 ,LOCK.notifyAll()*/
private Map<Integer,Condition> conditionMap = new HashMap<>();
public ShareResource_test(int threadNum,int MAX_VALUE) {
this.threadNum = threadNum;
this.MAX_VALUE = MAX_VALUE;
for (int i = 1; i <= threadNum ; i++) {
conditionMap.put(i,lock.newCondition());
}
}
public AtomicInteger getCount() {
return count;
}
//線程A執行
public void printx(int x) {
lock.lock();
try {
/**1. 判斷,是線程 x 纔會不進循環等待走下面 */
while (currentThreadNumber != x) {
// 就要停止一直等--
Condition c = searcherCondiionByThreadNum(x);
c.await();/** 等待並釋放當前xxx*/
}
/** 2. 幹活--》執行累加數 操作**/
for (int i = 1; i <= 5; i++) {
if(count.get()>= MAX_VALUE){
break;//或者 contione; 不可用 return;/** 注意:不能直接return,會造成死鎖--》因爲下面的threadNumber 線程必須切換通知喚醒*/
}
count.incrementAndGet();/** 執行加一操作 **/
System.out.println(Thread.currentThread().getName() + "--currentValue is: " + count );
}
/** 3. condition.signal(); 通知下一個xx--按指定通知線程--標誌位2代表精確喚醒2線程*/
Map<Integer, Condition> nextSingleThread = judgNextSingalThreadByPrevious(x);
/** 這一步很重要!!,改變當前xxx **/
currentThreadNumber = getKeyOrNull(nextSingleThread);
System.out.println("+++ single other thread do +++++");
getFirstOrNull(nextSingleThread).signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
/** 可以考慮個對象封裝xxx */
private Condition searcherCondiionByThreadNum(int x) {
return conditionMap.get(x);
}
/**
* 獲取map中第一個key值
*
* @param map 數據源
* @return
*/
private static <k ,v> k getKeyOrNull(Map<k, v> map) {
k obj = null;
for (Map.Entry<k, v> entry : map.entrySet()) {
obj = entry.getKey();
if (obj != null) {
break;
}
}
return obj;
}
/**
* 獲取map中第一個數據值
*
* @param map 數據源
* @return
*/
private static <k ,v> v getFirstOrNull(Map<k, v> map) {
v obj = null;
for (Map.Entry<k, v> entry : map.entrySet()) {
obj = entry.getValue();
if (obj != null) {
break;
}
}
return obj;
}
/**主要是這個通知下一個條件可以用的線程 邏輯不一樣--> 其實應該也可以寫爲動態的!!-- */
private Map<Integer,Condition> judgNextSingalThreadByPrevious(int x) {
HashMap<Integer, Condition> result = new HashMap<>();
int nextConditionNum;
if (x < this.threadNum)
{
nextConditionNum = x+1; /** 指向下一個線程 */
}
else
{
nextConditionNum = 1;/**最後一個則是 指向第一個線程 */
}
Condition nextCondition = this.conditionMap.get(nextConditionNum);
result.put(nextConditionNum,nextCondition);
return result;
}
}
/**--- main ----
* @Description: 多線程之間按順序調用,實現A->B->C
* x 個線程啓動,要求如下:
* <p>
* AA打印12345,BB打印678910,CC打印11.12.13.14.15
* 接着
* AA打印,BB打印,CC打印
* ......來x輪 --》直到打印到 100 結束!
*/
public class ThreadOrderAccess_test {
/**最大累加計數值*/
public static final int MAX_VALUE = 100;
/** 任意可變化不同線程數良*/
private static final int threadNum = 5;
public static void main(String[] args) {
ShareResource_test sr = new ShareResource_test(threadNum, MAX_VALUE);
for (int i = 1; i <= threadNum ; i++) {
final int a =i;
new Thread(() -> {
while (sr.getCount().get() < MAX_VALUE) {
sr.printx(a+0);
}
}, i+"").start();
}
}
}