java - Print List of string Multithreaded -
i have list should print values on multithread based on thread count.
public class launcher { public static void main(string[] args) { list<string> tests = arrays.aslist("a", "b", "c", "d"); final int thread_count = 2; (int = 0; < thread_count; i++) new demo(tests.get(i)); }} public class demo implements runnable { thread t; string tests; demo(string tests) { t = new thread(this); this.tests = tests; t.start(); } @override public void run() { system.out.println("started "+tests); try { thread.sleep(5000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("stop"); }}
it prints
started a
started b
stop
stop
but want print
started a
started b
stop
stop
started c
started d
stop
stop
you cannot start c
and d
because set final int thread_count = 2;
, loop stops after reaching i < thread_count
. if want startet in 2 step loop, enhance loop like:
for(int s = 0; s < 3; s+2){ (int = 0; < thread_count; i++) new demo(tests.get(i+s));} }
Comments
Post a Comment