Заготовка планировщика.

This commit is contained in:
2023-09-26 22:57:18 +03:00
parent 6df3eb05ee
commit 0026701347
5 changed files with 118 additions and 7 deletions

8
.idea/workspace.xml generated
View File

@@ -7,11 +7,11 @@
</component>
<component name="ChangeListManager">
<list default="true" id="e42177c3-2328-4b27-8a01-35779b2beb99" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/SapforTestingSystem/SapforTestingPlaner/SapforTestingPlanner.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/SapforTestingSystem/ThreadTask/ThreadTask.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/SapforTestingSystem/ThreadsPlanner/ThreadsPlanner.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Common/ModesSupervisors/PackageModeSupervisor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Common/ModesSupervisors/PackageModeSupervisor.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Common/Utils/Utils.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Common/Utils/Utils.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/SapforTestingSystem/SapforTest/SapforTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/SapforTestingSystem/SapforTest/SapforTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Visual_DVM_2021/Passes/All/StartSapforTests.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Visual_DVM_2021/Passes/All/StartSapforTests.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Common/UI/Menus_2023/MainMenuBar/MainMenuBar.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Common/UI/Menus_2023/MainMenuBar/MainMenuBar.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />

View File

@@ -4,6 +4,7 @@ import Common.UI.Menus_2023.MenuBarButton;
import Common.UI.Menus_2023.VisualiserMenuBar;
import Common.UI.UI;
import Repository.Component.PerformanceAnalyzer.PerformanceAnalyzer;
import SapforTestingSystem.SapforTestingPlaner.SapforTestingPlanner;
import Visual_DVM_2021.Passes.PassCode_2021;
import Visual_DVM_2021.Passes.Pass_2021;
@@ -50,17 +51,17 @@ public class MainMenuBar extends VisualiserMenuBar {
//-
setPreferredSize(new Dimension(0, 30));
//---
/*
add(new MenuBarButton() {
{
setIcon("/icons/Apply.png");
setToolTipText("Test");
addActionListener(e -> {
Current.getProject().hasSubdirectories();
SapforTestingPlanner planner =new SapforTestingPlanner();
planner.Start();
});
}
});
*/
//---
ShowProject(false);
}

View File

@@ -0,0 +1,19 @@
package SapforTestingSystem.SapforTestingPlaner;
import Common.Global;
import Common.Utils.Utils;
import SapforTestingSystem.ThreadsPlanner.ThreadsPlanner;
public class SapforTestingPlanner extends ThreadsPlanner {
public SapforTestingPlanner() {
super(2000, 4);
//--
for (int i = 1; i <= 20; ++i) {
int thread_number = i;
addThread(() -> {
for (int j=1; j<=4; ++j) {
Global.Log.Print("thread " + thread_number+" j="+j);
Utils.sleep(1000);
}
});
}
}
}

View File

@@ -0,0 +1,10 @@
package SapforTestingSystem.ThreadTask;
import Common.Utils.Utils;
public class ThreadTask {
public int id = Utils.Nan;
public Thread thread;
public ThreadTask(int id_in, Runnable runnable){
id = id_in;
thread = new Thread(runnable);
}
}

View File

@@ -0,0 +1,81 @@
package SapforTestingSystem.ThreadsPlanner;
import Common.Global;
import Common.Utils.InterruptThread;
import java.util.LinkedHashMap;
import java.util.Vector;
public abstract class ThreadsPlanner {
//-->
Thread interruptThread = new InterruptThread(5000, () -> {
System.exit(0);
return null;
});
protected int maxKernels;
protected int kernels;
//---
protected int threadMaxId = 0;
protected int wait_ms;
protected LinkedHashMap<Integer, Thread> threads = new LinkedHashMap<>();
protected Vector<Integer> activeThreads = new Vector<>();
protected Vector<Integer> waitingThreads = new Vector<>();
//--
public ThreadsPlanner(int wait_ms_in, int maxKernels_in) {
wait_ms = wait_ms_in;
maxKernels = maxKernels_in;
kernels = maxKernels;
}
//--
public void Start() {
Global.Log.Print("Planner started");
try {
//--
while (!waitingThreads.isEmpty() || !activeThreads.isEmpty()) {
Global.Log.Print("Waiting: " + waitingThreads.size() + "; Running: " + activeThreads.size() + ".");
checkActiveThreads();
tryStartThreads();
Thread.sleep(wait_ms);
}
//--
} catch (Exception exception) {
exception.printStackTrace();
Global.Log.PrintException(exception);
}
Global.Log.Print("Planner finished");
finalize();
}
protected void checkActiveThreads() throws Exception {
Vector<Integer> toExclude = new Vector<>();
//--
for (int i : activeThreads) {
Thread thread = threads.get(i);
if (!thread.isAlive()) {
toExclude.add(i);
kernels++;
}
}
activeThreads.removeAll(toExclude);
//--
}
protected void tryStartThreads() throws Exception {
Vector<Integer> toExclude = new Vector<>();
//-
for (int i : waitingThreads) {
if (kernels > 0) {
Thread thread = threads.get(i);
thread.start();
activeThreads.add(i);
kernels--;
toExclude.add(i);
} else break;
}
waitingThreads.removeAll(toExclude);
}
protected void finalize() {
}
protected void addThread(Runnable runnable) {
Thread thread = new Thread(runnable);
threads.put(threadMaxId, thread);
waitingThreads.add(threadMaxId);
threadMaxId++;
}
}