Jump to content

Code running time : Single vs multiple cores

- - - - -

  • Please log in to reply
5 replies to this topic

#1
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
I'm in the midst of programming a project that will be very resource intensive, so I decided to write a small program and run it on Amazon machines, both single and dual core. I've read that the JVM automatically makes use of multiple cores if they exist, so I would think I wouldn't need to make multiple threads.

However, after having run the program on some machines, I do get a small improvement but nowhere near what I would expect. So am I wrong to think that the JVM will automatically split my program onto multiple cores even if it's just one class and therefore one thread? Because if I'm not wrong, there must be something wrong with my code and I'll have to look into it then.

And in case I need to make a thread myself for each core, then how do I go about sharing resources between the threads? The resources are some Lists that have so many objects that they take up about 0,5 GB, and they will be moderated during a program's run, thus each thread would need a copy but that would consume too much memory.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Yes, you will need to make your own threads.

As for sharing the List, Collections - synchronized list might just be enough, depending on what you're actually doing.

#3
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
Alright, I've made another test example now to use on the Amazon machines.

public class Test implements Runnable {
    public void run() {
        for (long j = 0; j < 10000000000L; j++) {}
    }
    
    public static void main(String[] args) throws InterruptedException {
        long time = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new Test());
            t.start();
            t.join();
        }
        System.out.println(System.currentTimeMillis() - time);
    }
}

However, it takes about 45 seconds whether I use threads or not, which I find odd as I have a dual core processor, so I'd expect the program to take about half as much time now since it should split the work between the two cores. So where am I going wrong?

Edit:
It just occured to me that it would probably be because the for loop isn't actually doing anything that takes time. If I were to put some code in there that took some time to complete, I'd likely see a difference. So I'll try that now.

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Will threads "really" work on separate processes when you need? I'm not too keen on this, but I thought multiple threads ran in 1 process.

Something that might help you and was recently added to Java 7 was fork and join.
Fork/Join (The Java™ Tutorials > Essential Classes > Concurrency)

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
In this situation using .join() of course doesn't cause a speed up. If you use .join() for every thread you may as well not use threads at all.

#6
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
wim DC, yeah I thought join just set a boolean variable that would be checked or something like that, not actually wait for the thread to finish. So after reading up on it, I create the threads and start them in one loop and then join them in another.

After having done that, I did get it to work. Next step is looking into thread pools.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users