java - using .join() for a group of threads gives rise to an unexpected result -
i need execute grouping of threads @ same time , wait them finish before resuming main thread. custom class myrunnable:
class myrunnable implements runnable{ int value; myrunnable(int value){ this.value = value; } @override public void run(){ system.out.println("started thread " + value); system.out.println("finished thread " + value); } } thread[] mythreads; int total = 25; int size = 5; while(total > 0){ total -= 5; size = 5; mythreads = new thread[size]; for(int = 0; < size; i++){ mythreads[i] = new thread(new myrunnable (i)); } for(thread t:mythreads){ t.start()j } for(thread t:mythreads){ try{ t.join(); } catch(interruptedexception ex) { system.out.println("error"); } } system.out.println("set finished"); } i need grouping of 5 threads execute @ time , main thread go on when threads have finished execution. code above not work calls .join seem ignored. tried giving each of threads in grouping random sleep time see if unexpected result due threads finishing faster phone call .join() made. result still same.
i tweaked (to add together println after each join call), compiled , ran application, , see no evidence join beingness skipped. code , output below.
code:
package test; public class threadtest { static class myrunnable implements runnable { int value; myrunnable(int value) { this.value = value; } @override public void run() { system.out.println("started thread " + value); system.out.println("finished thread " + value); } } public static void main(string[] args) { thread[] mythreads; int total = 25; int size = 5; while (total > 0) { total -= 5; size = 5; mythreads = new thread[size]; (int = 0; < size; i++) { mythreads[i] = new thread(new myrunnable(i)); } (thread t : mythreads) { t.start(); } (thread t : mythreads) { seek { t.join(); system.out.println("joined"); } grab (interruptedexception ex) { system.out.println("error"); } } system.out.println("set finished"); } } } output:
started thread 0 finished thread 0 started thread 2 started thread 1 finished thread 1 finished thread 2 started thread 3 finished thread 3 joined joined joined joined started thread 4 finished thread 4 joined set finished started thread 0 started thread 1 started thread 2 finished thread 2 started thread 3 finished thread 0 finished thread 3 finished thread 1 joined joined joined joined started thread 4 finished thread 4 joined set finished started thread 0 finished thread 0 started thread 1 started thread 2 finished thread 1 finished thread 2 joined started thread 4 finished thread 4 started thread 3 joined joined finished thread 3 joined joined set finished started thread 0 started thread 3 finished thread 3 started thread 4 started thread 1 started thread 2 finished thread 1 finished thread 4 finished thread 0 finished thread 2 joined joined joined joined joined set finished started thread 0 started thread 1 finished thread 0 finished thread 1 started thread 2 finished thread 2 started thread 4 joined joined joined started thread 3 finished thread 4 finished thread 3 joined joined set finished java multithreading
No comments:
Post a Comment