Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    (Beginner) Help getting 2 threads to run at full potential (just read)

    Hi, I'm trying to brush up on Java skills to possibly enroll back into school. I'm trying to play with multithreading to learn how to do it and what advantage it has. I'm confused by my results.

    - I made a small Runnable brute force sudoku board generator
    - I start the runnable & I measure it's performance in "million boards per second" (mbps for this discussion)
    - I get an avg of about 52 mbps (core2duo 2ghz, 1 core maxed, other idle)
    - I can run 2 instances of this java program and the performance per-program will be the same as 1 instance - giving me >100mbps total
    - When I simply start 2 threads of the brute force object, my performance is not as expected. It either performs WORSE than 1 thread, or about the same.

    Code:
    public class Main {
        static BruteForceThread bf,bf2;
        static Thread t1,t2;
    
        public static void main(String[] args) {
    
            bf = new BruteForceThread();
            bf2 = new BruteForceThread();
            t1 = new Thread(bf);
            t2 = new Thread(bf2);
            t1.start();
            t2.start();
        }
    }
    class BruteForceThread implements Runnable{
        int[][] board;
        int boards;
    
        public BruteForceThread(){
            board = new int[9][9];
            boards = 0;
        }
    
        public void run() {
            long startTime = System.nanoTime();
            bruteForce(0,0);
            double estimatedTime = (System.nanoTime() - startTime)/1000000000d;
            System.out.println("250 million in " + (estimatedTime+"").substring(0, 4) + "s = " + (250000000/estimatedTime/1000000+"").substring(0, 4) + " million boards/s");
        }
    
        public void bruteForce(int i, int j){
    
            if(i==9){
                boards++;
                return;
            }
            for(int k=1;k<10&&boards<250000000;k++){
                board[i][j] = k;
                bruteForce(i+((j+1)/board[i].length),(j+1)%board[0].length);
            }
        }
    }
    Each BruteForceThread runs until it spits out 250million boards and then prints its time. When running the program with only t1 started, we get:
    Code:
    250 million in 4.92s = 50.8 million boards/s
    If I start t2, I get:
    Code:
    250 million in 6.91s = 36.1 million boards/s
    250 million in 11.0s = 22.6 million boards/s
    CPU usage is 100% when the 2 threads are running. I want to know what I am doing wrong to not get the performance scaling like I had when I ran the program twice.

    Thanks for any help.

  • #2
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Keep in mind that this program is not intended to solve sudoku puzzles. This is my first thread in these forums, so I don't know how much spam comments I will receive. All help appreciated.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •