Solved [SOLVED] Java and multithreading

Hi there

I use a java program which runs heavy math simulations, though it is using only one CPU core out of eight. Looking at the Java docs I see that it is up to the underlying system to distribute threads across all CPUs/cores. So, how can I do this under FreeBSD?
 
Re: Java and multithreading

Java itself should be able to run on multiple cores but it's possible the java application is only single threaded.
 
Re: Java and multithreading

Unfortunately this one app is a multithreaded one as it burns all cores on my MacBook ... There is however one difference: the Java version on the MacBook is 1.7 whereas the one on the FreeBSD box is 1.6. Could this cause the problem?
 
Re: Java and multithreading

I don't think it matters but you could try 1.7 of course.
 
Re: Java and multithreading

The detection routine of the said application which determines how many cores you have might be faulty. Maybe it does not work for FreeBSD correctly and it spawns only one thread at a time.
 
Re: Java and multithreading

Is there any way to influence this or force a specific number of CPUs? I have little to no experience with Java, maybe there is a config file somewhere?
 
Re: Java and multithreading

Do you have the source code? If yes, start looking for the words "Thread" and "Runnable", in particular looking for classes that say "extends Thread" or "implements Runnable". Hopefully, there are good comments, and hopefully the source code is well organized and compact. FIgure out how objects of these classes are created, and look for if statements in that vicinity.

Failing that: If you know the basics of java programming, write yourself a small test program, which spawns a variable number of threads (# of threads could be a command line parameter), and starts each thread. The threads could be doing some mindless compute-intensive task (loop a few million times, doing lots of sines, cosines, square roots, integer multiplication and division, bit shifts, what have you). Arrange the count of operations so they run for a minute or so, which gives you enough time to observe the behavior. Then run that program.

For an expert java programmer (which I'm no longer, having given up on java over a decade ago), this task should take 15 minutes. For a beginner, it's probably an afternoon.

Obviously, you can also search the web to find a pre-built java program that "soaks the CPU".
 
Re: Java and multithreading

How about something like this?
Code:
class NewThread implements Runnable {
  Thread t;
  int delay;
  int[] aBunch;
  java.util.Random randGen = new java.util.Random();

  NewThread(String name, int delay) {
    t = new Thread(this, name);
    this.delay = delay*10;
    aBunch = new int[10000000];
    System.out.println("Child thread: " + t);
    t.start();
  }

  public void run() {
    try {
      for (int i = 20; i>0; i--) {
        System.out.println(t.getName()+": " + i);
        for (int j = 0; j<aBunch.length; j++)
          aBunch[j] = randGen.nextInt(1000);
        int sumTotal = 0;
        for (int k : aBunch)
          sumTotal = sumTotal + i;
        int avgEnd = sumTotal / aBunch.length;
        Thread.sleep(delay);
      }
    } catch (InterruptedException e) {
      System.out.println("Child interrupted.");
      Thread.currentThread().interrupt();
    }
    System.out.println("Exiting child thread " + t.getName());
  }
}

public class ThreadDemo {
  public static void main(String args[]) {
    java.util.Random delayGen = new java.util.Random();

    NewThread one = new NewThread("One", delayGen.nextInt(10));
    NewThread two = new NewThread("Two", delayGen.nextInt(10));
    try {
     one.t.join(); 
     two.t.join(); 
    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted.");
      Thread.currentThread().interrupt();
    }
    System.out.println("Main thread exiting.");
  }
}

To test it:
Code:
javac ThreadDemo.java
java ThreadDeom
With top running in the "show threads" mode (H), I see both CPUs in my dual-CPU laptop being kept busy with either openjdk6 or openjdk7.
 
Re: Java and multithreading

Thank you very much for the quick example. I ran it with openjdk6 and, yes indeed, all CPUs get busy. Then the quirk could be caused by icedtea-web (sorry, forgot to mention that it is a webstart app).
 
Re: Java and multithreading

OK, guys, that's it! Looking at this page I read that:
Currently icedtea-web only employs 2 worker threads for all applet tasks.
Pfui! The box isn't suitable to become a hackintosh, so I'll have to install Windows :(
 
Back
Top