-5

This is a very peculiar question. I'm wondering the best way to create a lot of lag when running this Java program. No matter the speed of the computer, I want this Java program to cause annoying processor lag until the program is stopped. How am I guaranteed to achieve this? What are the most efficient methods?

So far the only thing I've been moderately successful with is

while (true) {}

But even that can sometimes be unreliable if the user's computer is fast.

I am using this to perform some scientific experiments on how much a group of computers can handle.

Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
  • This is kind of a ridiculous question, but OK. Create a bunch of threads and run them all, with all their executions being an infinite loop. It'll create a lot of "lag" then eventually crash the JVM – Kon Oct 17 '13 at 01:11
  • Haha yes this is very weird but I'm doing some experiments to see how much certain computers can handle. – Dylan Wheeler Oct 17 '13 at 01:12
  • @JavaCoder-1337 Although I understand what you mean by "lag," you should clarify that you want to essentially lag the processor, not simply delay the program's execution. You should also mention why you are trying to do this. Add both additions to the original question. – FThompson Oct 17 '13 at 01:13
  • Note that the JIT could potentially optimize away your `while` loop - since there are no side effects, it can potentially determine that the whole loop is unnecessary. Incrementing a static variable in the loop would be a bit better, in that regard. – dimo414 Oct 17 '13 at 01:21

3 Answers3

0

You could also just sleep the current thread. This doesn't create lag, but it'll give the program that effect.

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

More information

Oleksi
  • 12,947
  • 4
  • 56
  • 80
0

You might appreciate the utility I posted recently addressing Techniques for causing consistent GC Churn. I use this utility to simulate load on a system during benchmarks, which sounds quite similar to what you're looking to do.

It creates and destroys a large number of objects for a set period of time, or continuously in the background, and can be configured to put varying amounts of strain on the JVM. Try running several at once to be extra mean :)

The full code is available here: GC Churn Gist.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
0

I'm not sure I do understand exactly what you want, but it is improbable that you will get it using a sungle thread and an endless empty loop. First, that program will use at most one system thread, and therfore, a single CPU core. Second, the hotspot compiler is likely to optimize away your empty loop (or any arithmetic operation or assignement to a variable that will never be used). So what ever you want to accomplish, you need to do something that the optimizer won't attemp to improve. So...

If you want to slow down the execution of a program (rather than the computer), then spread around some Thread.sleep(1000). The JVM will yield that time back to the OS.

If your intention is to have the JVM do some work while waiting for some external event (as in "computing bogomips of the system"), then you should run code that will have side effect, so that the optimzer won't remove useless variable allocations. You may, for example, do some arithmetic operations on an array, then System.out.print() (or whatever else...) any value from that array. Note however that your process will still be limited to a single thread, and that if your process performs no IO operation for a long period, then it will most likely be depriorized by the operating system, so dont expect too much reliability from that type of measurement technique.

If you're looking at stalling the whole system, then you must run several threads, and have them do either intensive memory allocations, IO operations, or external program execution. You may for example run thousands of threads, each od them running a loop that execute an external Java program that allocate huge buffers, then write the content of that buffer to a file... Well, you get the idea ;)

dimo414
  • 47,227
  • 18
  • 148
  • 244
James
  • 4,211
  • 1
  • 18
  • 34