Skip to content
Snippets Groups Projects
Commit 0bae62ba authored by 1-211250088-shajianwei's avatar 1-211250088-shajianwei
Browse files

3.3 compiler and executor use strategy, add compiler to thread pool

parent 40983eab
No related branches found
No related tags found
1 merge request!4Master
......@@ -4,6 +4,8 @@ import org.example.answerSheet.AnswerSheet;
import org.example.exam.Exam;
import org.example.exam.ExamFactory;
import org.example.grader.ExamGrader;
import org.example.grader.judgeStrategy.ProgrammingStrategy;
import org.example.programmingThreadPool.ProgrammingThreadPool;
import java.io.BufferedWriter;
import java.io.File;
......@@ -57,6 +59,7 @@ public class Main {
}
}
}
ProgrammingStrategy.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -33,16 +33,19 @@ public class ProgrammingStrategy implements JudgeStrategy{
compileStrategy = new JavaCompileStrategy();
executeStrategy = new JavaExecuteStrategy();
}
boolean compileStatus = compileStrategy.compile(answer.getAnswer());
if (!compileStatus) {
return 0; // 编译失败,直接返回0分
Future<Boolean> compileStatus = programmingThreadPool.submit(() ->compileStrategy.compile(answer.getAnswer()));
try {
if (!compileStatus.get()) {
return 0; // 编译失败,直接返回0分
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
String fileName = answer.getAnswer().substring(answer.getAnswer().lastIndexOf('/') + 1);
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
Sample[] samples = ((ProgrammingQuestion) question).getSamples();
// 创建线程池
List<Future<Boolean>> futures = new ArrayList<>();
// 提交所有执行任务到线程池
......@@ -57,12 +60,28 @@ public class ProgrammingStrategy implements JudgeStrategy{
try {
// 如果有任何一个样例执行不通过,返回0分
if (!future.get()) {
// 取消所有尚未完成的任务
for (Future<Boolean> f : futures) {
if (!f.isDone()) {
f.cancel(true);
}
}
return 0;
}
} catch (Exception e) {
// 取消所有尚未完成的任务
for (Future<Boolean> f : futures) {
if (!f.isDone()) {
f.cancel(true);
}
}
return 0;
}
}
return question.getPoints(); // 返回满分
}
public static void shutdown() {
programmingThreadPool.shutdown();
}
}
......@@ -41,7 +41,6 @@ public class JavaExecuteStrategy implements ExecuteStrategy{
return false;
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return false;
}
return true;
......
......@@ -5,17 +5,16 @@ import java.util.List;
import java.util.ArrayList;
public class ProgrammingThreadPool {
private final List<Thread> workers;
private final List<Thread> threads;
private final BlockingQueue<Runnable> queue;
public ProgrammingThreadPool(int numberOfThreads) {
queue = new LinkedBlockingQueue<>();
workers = new ArrayList<>();
threads = new ArrayList<>();
for (int i = 0; i < numberOfThreads; i++) {
Thread worker = new Thread(this::run);
worker.start();
workers.add(worker);
Thread thread = new Thread(this::run);
thread.start();
threads.add(thread);
}
}
......@@ -25,9 +24,10 @@ public class ProgrammingThreadPool {
Runnable task = null;
synchronized (queue) {
if (!queue.isEmpty()) {
task = queue.take();
task = queue.poll();
} else {
queue.wait();
}
queue.notifyAll();
}
if (task != null) {
task.run();
......@@ -42,12 +42,12 @@ public class ProgrammingThreadPool {
FutureTask<V> task = new FutureTask<>(callable);
synchronized (queue) {
queue.add(task);
queue.notifyAll();
queue.notify();
}
return task;
}
public void shutdown() {
workers.forEach(Thread::interrupt);
threads.forEach(Thread::interrupt);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment