Skip to content
Snippets Groups Projects
Unverified Commit 4bc0c280 authored by 1-211250088-shajianwei's avatar 1-211250088-shajianwei
Browse files

4.2 end after time limit

parent e1331413
No related branches found
No related tags found
1 merge request!7Master
......@@ -27,7 +27,7 @@ public class ProgrammingStrategy implements JudgeStrategy{
@Override
public int calculateScore(Question question, Answer answer) {
ProgrammingThreadPool programmingThreadPool = ProgrammingThreadPool.getInstance();
ProgrammingThreadPool programmingThreadPool = new ProgrammingThreadPool(5);
if (answer.getAnswer().endsWith("java")) {
preProcessStrategy = new JavaPreProcessStrategy();
executeStrategy = new JavaExecuteStrategy();
......@@ -35,10 +35,12 @@ public class ProgrammingStrategy implements JudgeStrategy{
Future<Boolean> compileStatus = programmingThreadPool.submit(() -> preProcessStrategy.preProcess(answer.getAnswer()));
try {
if (!compileStatus.get()) {
compileStatus.cancel(true);
return 0; // 编译失败,直接返回0分
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
compileStatus.cancel(true);
return 0;
}
String fileName = answer.getAnswer().substring(answer.getAnswer().lastIndexOf('/') + 1);
......@@ -50,7 +52,7 @@ public class ProgrammingStrategy implements JudgeStrategy{
// 提交所有执行任务到线程池
for (Sample sample : samples) {
String finalFileName = fileName;
Callable<Boolean> task = () -> executeStrategy.execute(finalFileName, sample);
Callable<Boolean> task = () -> executeStrategy.execute(finalFileName, sample, ((ProgrammingQuestion) question).getTimeLimit());
futures.add(programmingThreadPool.submit(task));
}
......@@ -58,10 +60,16 @@ public class ProgrammingStrategy implements JudgeStrategy{
for (Future<Boolean> future : futures) {
try {
// 如果有任何一个样例执行不通过,返回0分
if (!future.get()) {
if (!future.get(((ProgrammingQuestion) question).getTimeLimit(), TimeUnit.MILLISECONDS)) {
for (Future<Boolean> f : futures) {
f.cancel(true);
}
return 0;
}
} catch (Exception e) {
for (Future<Boolean> f : futures) {
f.cancel(true);
}
return 0;
}
}
......
......@@ -3,5 +3,5 @@ package org.example.grader.judgeStrategy.executeStrategy;
import org.example.question.sample.Sample;
public interface ExecuteStrategy {
boolean execute(String fileName, Sample sample);
boolean execute(String fileName, Sample sample, int timeLimit);
}
......@@ -7,22 +7,31 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
public class JavaExecuteStrategy implements ExecuteStrategy{
@Override
public boolean execute(String fileName, Sample sample) {
public boolean execute(String fileName, Sample sample, int timeLimit) {
String classpath = "";
try {
classpath = Paths.get(getClass().getClassLoader().getResource("cases").toURI()).toString() +
System.getProperty("file.separator") + "compileResult";
} catch (URISyntaxException e) {
e.printStackTrace();
return false;
}
Process runProcess;
Process runProcess = null;
try {
String command = "java -cp " + classpath + " " + fileName + " " + sample.getInput();
// 设置classpath,并执行指定的类
runProcess = Runtime.getRuntime().exec(command);
boolean finished = runProcess.waitFor(timeLimit, TimeUnit.MILLISECONDS);
if (!finished) {
// 如果超时,则尝试终止进程
runProcess.destroy();
return false; // 返回false表示执行失败
}
BufferedReader inputReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
String output = "";
String line;
......@@ -30,6 +39,7 @@ public class JavaExecuteStrategy implements ExecuteStrategy{
output += line;
}
inputReader.close();
// 检查程序是否异常退出
int exitVal = runProcess.waitFor();
if (exitVal == 0) {
......@@ -40,6 +50,9 @@ public class JavaExecuteStrategy implements ExecuteStrategy{
return false;
}
} catch (IOException | InterruptedException e) {
if (runProcess != null && runProcess.isAlive()) {
runProcess.destroy();
}
return false;
}
return true;
......
......@@ -7,7 +7,7 @@ import java.nio.file.Paths;
public class JavaPreProcessStrategy implements PreProcessStrategy {
@Override
public boolean preProcess(String filePath) {
Process compileProcess;
Process compileProcess = null;
try {
// 使用-d选项指定编译后的输出目录
compileProcess = Runtime.getRuntime().exec("javac -d " +
......@@ -18,7 +18,9 @@ public class JavaPreProcessStrategy implements PreProcessStrategy {
return true;
}
} catch (IOException | InterruptedException | URISyntaxException e) {
e.printStackTrace();
if (compileProcess != null && compileProcess.isAlive()) {
compileProcess.destroy();
}
}
return false;
}
......
......@@ -7,6 +7,7 @@ import java.util.ArrayList;
public class ProgrammingThreadPool {
private final List<Thread> threads;
private final BlockingQueue<Runnable> queue;
private static boolean isShutdown = false;
private final static ProgrammingThreadPool instance = new ProgrammingThreadPool(5);
......@@ -25,7 +26,7 @@ public class ProgrammingThreadPool {
}
private void run() {
while (true) {
while (!isShutdown) {
try {
Runnable task = null;
synchronized (queue) {
......@@ -38,9 +39,7 @@ public class ProgrammingThreadPool {
if (task != null) {
task.run();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} catch (InterruptedException ignore) {}
}
}
......@@ -54,6 +53,6 @@ public class ProgrammingThreadPool {
}
public void shutdown() {
threads.forEach(Thread::stop);
isShutdown = true;
}
}
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