Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2011, 02:15 AM   PM User | #1
andisblue
New to the CF scene

 
Join Date: Jan 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
andisblue is an unknown quantity at this point
(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.
andisblue is offline   Reply With Quote
Old 01-12-2011, 02:17 AM   PM User | #2
andisblue
New to the CF scene

 
Join Date: Jan 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
andisblue is an unknown quantity at this point
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.
andisblue is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:05 PM.


Advertisement
Log in to turn off these ads.