Reputation: 71
How can I reduce time for finishing all for loop task? For some reason, I have to used sleep(8)milliseconds.SO,finishing all loops it's need more than 2 hours in CORE i7 CPU and 8GB RAM PC.I'm novice in this field.
ExecutorService executorService = Executors.newCachedThreadPool();
//Task One
executorService.execute(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 10; j++) {
for (int k = 1; k <= 1200; k++) {
for (int l = 1; l <= 10; l++) {
try {
Thread.sleep(8);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("Task # A:" + i + " AF: " + j + " C:" + k + " CF:" + l);
}
}
}
}
}
});
I'm trying another way.This is the right way to reducing time? or suggest me to something better.
public class Ask1 {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
//Total customer 1200
//1-400 for
System.out.println("customer range 1 to 400");
executorService.execute(new TaskThread(2,10,400));
//401 - 800
System.out.println("customer range 401 to 800");
executorService.execute(new TaskThread(2,10,400));
//801-1200
System.out.println("customer range 801 to 1200");
executorService.execute(new TaskThread(2,10,400));
executorService.shutdown();
}
}
class AskThread implements Runnable {
int agent;
int finger;
int customer;
public AskThread(int agent, int finger,int customer){
this.agent = agent;
this.finger = finger;
this.customer = customer;
}
@Override
public void run() {
for (int i = 1; i <= agent; i++) { //changeable
for (int j = 1; j <= finger; j++) { //fixed
for (int k = 1; k <= customer; k++) { //changeable
for (int l = 1; l <= finger; l++) { //fixed
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("Task One # Agent:" + i + " Agent Finger: " + j + " Customer:" + k + " Customer Finger:" + l);
}
}
}
}
}
}
Upvotes: 0
Views: 383
Reputation: 3131
Let's expand on my comment a little bit. As I said, the example you have provided is way too abstract to give some specific advice how to improve performance.
From what I can tell, you wanted to spread the work equally between multiple threads. In most cases you can't just divide all the "dimensions" by x when creating x threads.
Imagine more concrete task: You have a 2 dimensional matrix, representing a chess board. You want to iterate over all the elements in the matrix and do some operation. You will end up with a loops something like this:
for (int i = 0; i < dim1; i++){
for (int j = 0; j < dim2; j++){
\\do something here
}
}
In this example, we have to iterate over dim1 * dim2
elements.
Let's say we want to divide the work between 4 threads. If you divide both dim1
and dim2
by 4, you end up with 4 by 4 matrix, that is 16 parts. It is the most visible when you imagine a physical chess board.
If you have 4 threads you have to divide the work into 4 (usually equal) pieces. In this case, you can either divide both dim1
and dim2
by 2 for a total of 4 parts, or only one dimension by 4.
Edit. Probably the same will apply to your problem. If you have x agents, each have 20 fingers, and y customers with 20 fingers each, you probably want to compare each with each.
For 4 threads you can't just take 1/4 of agents, 1/4 of their fingers and compare with some part of customers - and only 1/4 of their fingers. All you can do is to take 1/4 of agents, all 20 of their fingers and compare with all the fingers of all the customers.
Upvotes: 3