d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Another Multithread Question > Better Way To Wait
Add Reply New Topic New Poll
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Sep 30 2016 01:55am
so heres my code
theres a line that i am interested in
Code
while (!executor.isTerminated()) {

}

i use it to wait until everythings done before printing just wondering if theres a better way
Code
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Created by user on 9/28/2016.
*/
public class Router {


public static void main(String[] args) {
Branch P = new Branch('P');
Branch F = new Branch('F');
Branch M = new Branch('M');

ExecutorService executor = Executors.newFixedThreadPool(9);

Job[] jobs = new Job[9];
jobs[0] = new Job(P, 1, 'D', 60000);
jobs[1] = new Job(P, 3, 'P', 100000);
jobs[2] = new Job(P, 2, 'D', 75000);
jobs[3] = new Job(F, 1, 'P', 30000);
jobs[4] = new Job(F, 2, 'D', 150000);
jobs[5] = new Job(F, 3, 'P', 89000);
jobs[6] = new Job(M, 1, 'P', 200000);
jobs[7] = new Job(M, 2, 'D', 140000);
jobs[8] = new Job(M, 3, 'P', 135000);
for (Job job : jobs) {
executor.execute(job);
}
executor.shutdown();
while (!executor.isTerminated()) {

}
F.printBranch();
P.printBranch();
M.printBranch();
}
}


edit: this is meant for a single processor system, it doesnt seem to be necessary when run on a multiprocessor system

This post was edited by Ideophobe on Sep 30 2016 02:02am
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Sep 30 2016 09:46pm
Quote (carteblanche @ Sep 30 2016 05:14pm)


ya know i saw it in my ide but i was thrown off by the parameters
gold

i guess the common thing to do is just long.max,etc,etc yaknow
does technically put a time limit on it but i guess you get what you pay for

This post was edited by Ideophobe on Sep 30 2016 09:48pm
Member
Posts: 3,476
Joined: Jul 20 2015
Gold: 651.00
Oct 2 2016 01:02pm
I'm pretty sure you can join all threads at the end, that should wait until all threads are done running before continuing.

I did something similar, but I don't have access to the code at the moment to tell you exactly how to do it.
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Oct 4 2016 05:53pm
it would be nice to see that code if you can find it
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll