操作系统 & 图论传参一个AOE图,得到按其拓扑排序及权值执行临界区代码的线程数组
Posted 嘉然今天吃甚么
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了操作系统 & 图论传参一个AOE图,得到按其拓扑排序及权值执行临界区代码的线程数组相关的知识,希望对你有一定的参考价值。
import java.util.concurrent.Semaphore;
public class Solution
final static float INF = Float.POSITIVE_INFINITY;
static Semaphore[][] semaphores;
public static Thread[] AOEThreads(float[][] matrix)
semaphores = new Semaphore[matrix.length][matrix.length];
int v = 0;
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[0].length; j++)
if (matrix[i][j] != 0 && matrix[i][j] != INF)
v++;
semaphores[i][j] = new Semaphore(0);
Thread[] threads = new Thread[v];
int index = 0;
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[0].length; j++)
if (semaphores[i][j] != null)
int x = i;
int y = j;
float f = matrix[x][y];
threads[index++] = new Thread(() ->
try
for (int k = 0; k < semaphores.length; k++)
if (semaphores[k][x] != null)
// System.out.println(Thread.currentThread().getName() + "发现自己受制于vertex[" + k + "][" + x + "]");
semaphores[k][x].acquire();
// System.out.println(Thread.currentThread().getName() + "发现vertex[" + k + "][" + x + "]已开锁");
System.out.println(Thread.currentThread().getName() + "开始执行");
Thread.sleep((int) f * 1000);
System.out.println(Thread.currentThread().getName() + "执行了" + f + "妙后执行完毕");
for (int k = 0; k < semaphores.length; k++)
if (semaphores[y][k] != null)
semaphores[x][y].release();
// System.out.println(Thread.currentThread().getName() + "已解锁");
catch (Exception e)
e.printStackTrace();
, "vertex[" + x + "][" + y + "]");
return threads;
public static void main(String[] args)
float[][] matrix = new float[][]
0, 3, 2, INF, INF, INF,
INF, 0, INF, 2, 3, INF,
INF, INF, 0, 4, INF, 3,
INF, INF, INF, 0, INF, 2,
INF, INF, INF, INF, 0, 1,
INF, INF, INF, INF, INF, 0
;
Thread[] threads = AOEThreads(matrix);
for (Thread thread : threads)
thread.start();
以上是关于操作系统 & 图论传参一个AOE图,得到按其拓扑排序及权值执行临界区代码的线程数组的主要内容,如果未能解决你的问题,请参考以下文章