multithreading - Java - how to make these methods mutlithreaded? -
in terms of multi-threading noob. have written programs uses java thread nothing special.
current code single-threaded. reads data file, generate random numbers , check if numbers belong given inteval.
import java.io.*; import java.util.*; class generator { private double mean; private double variance; private long amountofnumberstogenerate; public generator(double mean, double variance, long amountofnumberstogenerate) { this.mean = mean; this.variance = variance; this.amountofnumberstogenerate = amountofnumberstogenerate; } double getmean() { return mean; } double getvariance() { return variance; } long getamountofnumberstogenerate() { return amountofnumberstogenerate; } } class interval { private double start; private double end; public interval(double start, double end) { this.start = start; this.end = end; } double getstart() { return start; } double getend() { return end; } } class parseddata { private vector<generator> generators; private vector<interval> intervals; public parseddata(vector<generator> generators, vector<interval> intervals) { this.generators = generators; this.intervals = intervals; } vector<generator> getgenerators() { return generators; } vector<interval> getintervals() { return intervals; } } class worker extends thread { public worker() { } } class start { static parseddata readdatafromfile(string path) throws ioexception { file file = new file(path); bufferedreader br = new bufferedreader(new filereader(file)); string line; line = br.readline(); string delimiter = "\\s+"; // generators long generatorsize = long.parselong(line); vector<generator> generators = new vector<generator>(); for(long =0; < generatorsize; i++) { line = br.readline(); scanner f = new scanner(line); f.uselocale(locale.us); //without line program wouldn't work on machines different locales f.usedelimiter(delimiter); generator g = new generator(f.nextdouble(), f.nextdouble(), f.nextint()); generators.add(g); } line = br.readline(); long intervalsize = long.parselong(line); vector<interval> intervals = new vector<interval>(); for(long = 0; < intervalsize; i++) { line = br.readline(); system.out.println(line); scanner f = new scanner(line); f.uselocale(locale.us); //without line program wouldn't work on machines different locales f.usedelimiter(delimiter); interval interval = new interval(f.nextdouble(), f.nextdouble()); intervals.add(interval); } br.close(); return new parseddata(generators, intervals); } static double boxmullermarsagliapolarrand(double mean, double variance) { double micro = mean; double sigma = math.sqrt(variance); double y, x, omega; random random = new random(); { x = random.nextdouble(); y = random.nextdouble(); omega = x * x + y * y; } while (!(0.0 < omega && omega < 1.0)); double sigma_sqrt = sigma * math.sqrt(-2.0 * math.log(omega) / omega); double g = micro + x * sigma_sqrt; // float h = micro + y * sigma_sqrt; return g; } ///////////////////////////////////////// // todo: refactor code multithreaded static vector<double> generaterandomnumbers(parseddata parseddata) { vector<double> generatednumbers = new vector<double>(); for(int = 0; < parseddata.getgenerators().size(); i++) { generator g = parseddata.getgenerators().get(i); for(long j = 0; j < g.getamountofnumberstogenerate(); j++) { double random = boxmullermarsagliapolarrand(g.getmean(), g.getvariance()); generatednumbers.add(random); } } return generatednumbers; } ///////////////////////////////////////// // todo: refactor code multithreaded static int[] checkintervals(parseddata parseddata, vector<double> generatednumbers) { int[] numberofhits = new int[parseddata.getintervals().size()]; for(int j = 0; j < parseddata.getintervals().size(); j++) { interval interval = parseddata.getintervals().get(j); for(int = 0; < generatednumbers.size(); i++) { if (interval.getstart() < generatednumbers.get(i) && generatednumbers.get(i) < interval.getend()) { numberofhits[j]++; } } } return numberofhits; } public static void main(string args[]) { int amountofthreads = integer.parseint(args[0]); string path = system.getproperty("user.dir") + "/input.dat"; parseddata parseddata = null; try { parseddata = readdatafromfile(path); } catch (ioexception e) { e.printstacktrace(); } system.out.println(parseddata.getgenerators().size()); system.out.println(parseddata.getintervals().size()); vector<double> generatednumbers = generaterandomnumbers(parseddata); int[] numberofhits = checkintervals(parseddata, generatednumbers); (int = 0; < numberofhits.length; i++) { interval interval = parseddata.getintervals().get(i); system.out.println("" + (i+1) + " " + interval.getstart() + " " + interval.getend() + " " + numberofhits[i]); } system.out.println(generatednumbers.size()); } }
i don't expect write/refactor code me.
but don't know how make methods multi-threaded :
///////////////////////////////////////// // todo: refactor code multithreaded static vector<double> generaterandomnumbers(parseddata parseddata) { vector<double> generatednumbers = new vector<double>(); for(int = 0; < parseddata.getgenerators().size(); i++) { generator g = parseddata.getgenerators().get(i); for(long j = 0; j < g.getamountofnumberstogenerate(); j++) { double random = boxmullermarsagliapolarrand(g.getmean(), g.getvariance()); generatednumbers.add(random); } } return generatednumbers; } ///////////////////////////////////////// // todo: refactor code multithreaded static int[] checkintervals(parseddata parseddata, vector<double> generatednumbers) { int[] numberofhits = new int[parseddata.getintervals().size()]; for(int j = 0; j < parseddata.getintervals().size(); j++) { interval interval = parseddata.getintervals().get(j); for(int = 0; < generatednumbers.size(); i++) { if (interval.getstart() < generatednumbers.get(i) && generatednumbers.get(i) < interval.getend()) { numberofhits[j]++; } } } return numberofhits; }
any (links, info etc) great.
best.
the easiest way make multithreaded use producer-consumer pattern, 1 producer reading data , sending blockingqueue
, , consumers reading data blockingqueue
(using take
) , processing using 2 static methods. way need minimal refactoring - static methods re-entrant / thread-safe (assuming vector
, parseddata
parameters aren't shared), don't need modified @ all.
Comments
Post a Comment