0

I am creating an online judge, for this I want that the submitted java code should not utilize the parallel processing of CPU. So, I want that the multi threading of the Java application to be disabled.

The application sent to the server would be just a single java file, it would be compiled to byte code and then executed, suppose the application is using threads, it is creating a thread, the application should be closed by throwing some exception like Permission Denied like error.

Nimit Bhardwaj
  • 827
  • 9
  • 19

1 Answers1

0

Instead of threads, use a tread pool. Create a threadpool with desired parallelism, for example, 1:

ExecutorService threadPool = Executors.newFixedThreadPool(1);

replace thread creation with submitting tasks to the thread pool:

Runnable r = ...
Thread t = new Thread(r);
t.start();
...
t.join();

replace with

Runnable r = ...
Future<Void> future = executorService.submit(r);
...
future.get();

this way you can create as many parallel tasks as you want.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • This answer does not help the OP because the OP is not the author of the code that might create threads. The OP wants to create a system to _judge_ the code (e.g., think "coding contest.") The contest rules say, "single threaded," and the OP wants an automated way to reject entries that don't obey that rule. – Solomon Slow Jun 10 '19 at 18:09
  • Yes, Its not what I wanted, I want to enforce the multithreading to only 1. I was trying to use apparmor for it, still no progress – Nimit Bhardwaj Jun 11 '19 at 06:40