java - Why is this program creating more threads than possible? -
this custom udtf in hive query, createlogtable udtf class using temp testing. creating 1 thread per file downloaded amazon s3 , waiting until thread becomes available before allocating file thread.
main test logic:
createlogtable clt = new createlogtable(); int numthreads = 2; int index = 0; downloadfilethread[] dlthreads = new downloadfilethread[numthreads]; (s3objectsummary osummary : bucketkeys.getobjectsummaries()) { while (dlthreads[index] != null && dlthreads[index].isalive()) { index += 1; index = index % numthreads; } dlthreads[index] = new downloadfilethread(clt , getbucket(osummary.getbucketname() + "/" + osummary.getkey()), getfile(osummary.getkey()), index); dlthreads[index].start(); index += 1; index = index % numthreads; } thread class (run() method):
try { system.out.println("creating thread " + this.threadnum); this.fileobj = this.s3client.getobject(new getobjectrequest(this.filepath, this.filename)); this.filein = new scanner(new gzipinputstream(this.fileobj.getobjectcontent())); while (this.filein.hasnext()) { this.parent.forwardtotable(filein.nextline()); } system.out.println("finished " + this.threadnum); } catch (throwable e) { system.out.println("downloading of " + this.filename + " failed."); } the while loop before thread creation should looping until finds null thread or dead thread until exits loop, in case new thread created , started. since included logging console, able observe process, output unexpected:
creating thread 0 creating thread 1 creating thread 0 creating thread 1 creating thread 0 creating thread 1 creating thread 0 ... creating thread 1 creating thread 0 creating thread 1 finished 0 finished 1 finished 1 finished 0 finished 1 finished 1 ... finished 0 finished 1 finished 0 finished 1 the above first few lines of output. issue more 2 threads created before threads complete tasks.
why happening , how can fix this?
try see example:
public static void main(string[] args) { executorservice executor = executors.newfixedthreadpool(5); (int = 0; < 10; i++) { runnable worker = new workerthread("" + i); executor.execute(worker); } executor.shutdown(); while (!executor.isterminated()) { } system.out.println("finished threads"); } it's thread pool using java 8. simple , esay way make using executors. staraight forward way make it.
Comments
Post a Comment