AKIWEB
AKIWEB

Reputation: 19612

Distribute Range of Numbers between each threads

Config File

ThreadSize = 10
StartRange = 1
EndRange = 1000

I have a config file above in which I have number of threads I want to use and the client instance is able to use ID range from 1 to 1000 and suppose the client threads is set at 10, so each thread would have range of 100 id's(basically by dividing end range with thread size) that it can use without stepping on other threads. so What I want is that each thread should use 100 id's from that range without stepping on other threads- for example

Thread1 will use 1 to 100 (id's)
// generate a random number between 1 to 100 and keep on printing values until it has generated all the random values between 1 to 100
Thread2 will use 101 to 200 (id's)
// generate a random number between 101 to 200 and keep on printing values until it has generated all the random values between 101 to 200
Thread3 will use 201 to 300 (id's)
// generate a random number between 201 to 300 and keep on printing values until it has generated all the random values between 201 to 300

-----
----
Thread10 will use 901 to 1000
// generate a random number between 901 to 1000 and keep on printing values until it has generated all the random values between 901 to 1000

I know how to write a multithreading program, but not sure how should I divide the range between various threads.

public static void main(String[] args) {

    for (int i = 1; i <= threadSize; i++) {
        new Thread(new ThreadTask(i)).start();
    }
}


class ThreadTask implements Runnable {
    private int id;

    public ThreadTask(int id) {
    this.id = id;
    }

    public synchronized void run() {

    }
}

Upvotes: 3

Views: 1364

Answers (2)

Deepalakshmi
Deepalakshmi

Reputation: 1

public class MyThread extends Thread {
    public static final int TOTAL_THREADS = 10;
    public static final int START_RANGE = 1;
    public static final int END_RANGE = 1000;
    public static final int N = (END_RANGE - START_RANGE + 1) / TOTAL_THREADS;
    int threadNo;
    static volatile int counter = 1;
    public final static Object obj = new Object();

    public MyThread(int threadNo) {
        this.threadNo = threadNo;
    }

    @Override
    public void run() {
        synchronized (obj) {
            while (counter <= 1000) {
                if (counter >= (START_RANGE + threadNo * N) && counter <= (START_RANGE + threadNo * N + N - 1)) {
                    System.out.println((this.threadNo + 1) + " prints " + counter++);
                    obj.notifyAll();
                } else {
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String args[]) {
        for (int i = 0; i < TOTAL_THREADS; i++) {
            System.out.println("Call thread : " + (i + 1));
            MyThread th = new MyThread(i);
            th.start();
        }
    }
}

Upvotes: 0

aioobe
aioobe

Reputation: 420951

Each thread gets N = (EndRange - StartRange + 1) / ThreadSize numbers.

Thread number i gets range (StartRange + i*N) - (StartRange + i*N + N - 1).

In your example N = (1000 - 1 + 1) / 10 = 100.

Thread i = 0 would get range (1 + 0*100) - (1 + 0*100 + 100 - 1) = 1 - 100

Thread i = 1 would get range (1 + 1*100) - (1 + 1*100 + 100 - 1) = 101 - 200

...

Upvotes: 4

Related Questions