2

First i would like to clear it that My question is different from the question How to check if the JIT compiler is off in Java which wants jo turn off JIT optimization. I want JIT to run but i want cpu-intensive task to be written in such a way that JIT can not optimize it anyway. Now i start explaining my problem. Following is a CPU-Bound task that is run by a Thread. I am sending this task to my server where a worker from thread pool picks it and runs it. I want workers to run this task at some fixed amount of time but as discussed in my first and second questions at stack-overflow , JIT interferes and optimizes code and as a result workers runs for arbitrary times. I don't want workers to run for different times. I want workers to run for fixed amount of time. What type of codes i should write in following class that cant not be optimized by JIT anyway. please help as this is part of my final year project.Working example would be highly appreciated!

import java.math.BigInteger;    
public class CpuBoundJob  implements Runnable {     
    public void run() {    
         BigInteger factValue = BigInteger.ONE;
            long t1=System.nanoTime();      
            for ( int i = 2; i <= 800; i++){
              factValue = factValue.multiply(BigInteger.valueOf(i));
            }
        long t2=System.nanoTime();              
        System.out.println("Service Time(ms)="+((double)(t2-t1)/1000000));
    }    
}
Community
  • 1
  • 1
Faisal Bahadur
  • 498
  • 3
  • 19
  • You'd have to put checks into each of (or sufficiently frequently into) your loop iterations and abort computation (return from `run`) when the time is up. Java has no good way to just kill a thread. – Thilo Jan 12 '16 at 02:21
  • 2
    Why? What purpose is to be served? – John Bollinger Jan 12 '16 at 02:23
  • 1
    Possible duplicate of [How to check if the JIT compiler is off in Java](http://stackoverflow.com/questions/9457405/how-to-check-if-the-jit-compiler-is-off-in-java) – Erwin Bolwidt Jan 12 '16 at 02:39
  • the purpose it to test the dynamic thread pool tuning strategies running on the server side. i submit thousands of CPU-bound tasks of same length to distributed strategy that will process the task and send responses. – Faisal Bahadur Jan 12 '16 at 02:43
  • Thread pool tuning strategy runs on cluster – Faisal Bahadur Jan 12 '16 at 02:44
  • 2
    Why not do something like `long t1 = System.nanoTime(); for (long i = 1; System.nanoTime() - t1 < 10000000L, i++) { foo *= i; }`, if you need a CPU-bound task taking a specific amount of time? – Louis Wasserman Jan 12 '16 at 02:50
  • It's the wrong question to ask anyhow. Much easier to have some warm-up code that allows the JIT to compile the method and only then start the benchmark. You still won't get around the scheduling differences and your current code will be optimized away because it's too simple (print the result for one simple solution around that) – Voo Jan 12 '16 at 07:23
  • @Voo Can you tell plz how much to warm up on run time. All this is done on run time. Client will decide the level of cpu-intensiveness and then send the jobs where each job can have different level of cpu-intensity. I just provided this code for easy understanding i could not provide the real long hard code. – Faisal Bahadur Jan 12 '16 at 08:31
  • Call the method a few hundred times in a loop at startup before doing anything else. – Voo Jan 12 '16 at 09:57
  • @Voo after warming up i get pritty smart service time of arround 35ms always.But when i run 2 tasks it becomes arround 70ms when 3 tasks simulatanously then it becomes arround 220. Why is it that when i increases threads the service times increases too. my system is i5-dual core with hyper-threading. – Faisal Bahadur Jan 12 '16 at 10:29
  • I was not expecting service time to increased till 4 simulatanous threads as i have 4 core processor. But it is strange. 4 threads on 4 processors should run w/o any overhead then why is it that service times are increasing? – Faisal Bahadur Jan 12 '16 at 10:32

1 Answers1

1

There is no way in Java to ensure that your code runs for a precise fixed amount of time. Besides interference from HotSpot (JIT), the garbage collector, other threads in your process and even the OS scheduler can all interfere with the timing of your application's execution. You can tweak JIT -XX switches to try to get the JIT to precompile your code, but I don't recommend this. You could also use G1 soft-realtime collector to smooth out GC pauses, but the bottom line is that if you really MUST depend on timing like this (and I'd suggest redesigning your application might be a better approach), you need to go down the stack at least to a C/C++ application and possibly all the way down to the device driver level if your requirements are truly real-time. Java simply isn't a real-time language.

Jonathan Locke
  • 126
  • 3
  • 4
  • There are niche efforts to make Java work in real-time environments: https://en.wikipedia.org/wiki/Real_time_Java – Nayuki Jan 12 '16 at 04:55