@@ -37,9 +37,6 @@ public class CommandLineInterface implements Runnable
3737 @ Override
3838 public void run ()
3939 {
40- // Don't start more threads than seeds we've asked for.
41- threads = Math .min (threads , targetCount );
42-
4340 System .out .println ("Config: " );
4441 System .out .println ("- threads: " + threads );
4542 System .out .println ("- radius: " + radius );
@@ -69,62 +66,36 @@ else if (map.equalsIgnoreCase("rocks"))
6966 }
7067 }
7168 final EnumSet <Layers > layers = EnumSet .copyOf (layersTmp );
72- final Thread [] threadArray = new Thread [threads ];
7369
74- if (!seeds .isEmpty ()) // We got seeds via CLI
70+ // Queue up all the seeds
71+ final ConcurrentLinkedQueue <WorldGen > queue = new ConcurrentLinkedQueue <>();
72+ if (seeds .isEmpty ())
7573 {
76- // Queue up all the seeds
77- final ConcurrentLinkedQueue <WorldGen > queue = new ConcurrentLinkedQueue <>();
78- for (String seed : seeds ) queue .add (new WorldGen (seed , radius , chunkSize , layers ));
79-
80- // Make a bunch of worker threads
81- for (int i = 0 ; i < threads ; i ++)
74+ while (targetCount -- != 0 )
8275 {
83- threadArray [i ] = new Thread (() -> {
84- while (!queue .isEmpty ())
85- {
86- WorldGen worldGen = queue .poll ();
87- if (worldGen == null ) continue ; // Just in case
88- worldGen .run ();
89- }
90- });
76+ queue .add (new WorldGen (null , radius , chunkSize , layers ));
9177 }
9278 }
93- else // We didn't get seeds via CLI, make some at random
79+ else
9480 {
95- final AtomicInteger goodCount = new AtomicInteger ();
96-
97- // Make a bunch of worker threads
98- for (int i = 0 ; i < Math .min (threads , targetCount ); i ++)
81+ // We got seeds via CLI
82+ for (String seed : seeds )
9983 {
100- threadArray [i ] = new Thread (() -> {
101- while (targetCount < 0 || goodCount .get () < targetCount )
102- {
103- WorldGen worldGen = new WorldGen (null , radius , chunkSize , layers );
104- worldGen .run ();
105- goodCount .incrementAndGet ();
106- }
107- });
84+ queue .add (new WorldGen (seed , radius , chunkSize , layers ));
10885 }
10986 }
11087
111- // Now actually start the threads
112- for (int i = 0 ; i < threads ; i ++) threadArray [i ].start ();
113-
114- // Output routine, quick and dirty.
115- while (true )
88+ // Make a bunch of worker threads
89+ for (int i = 0 ; i < Math .min (threads , queue .size ()); i ++)
11690 {
117- boolean done = true ;
118- for (int i = 0 ; i < threads ; i ++) if (threadArray [i ].isAlive ()) done = false ;
119- if (done ) break ;
120- try
121- {
122- Thread .sleep (100 );
123- }
124- catch (InterruptedException e )
125- {
126- e .printStackTrace ();
127- }
91+ new Thread (() -> {
92+ while (!queue .isEmpty ())
93+ {
94+ WorldGen worldGen = queue .poll ();
95+ if (worldGen == null ) continue ; // Just in case
96+ worldGen .run ();
97+ }
98+ }).start ();
12899 }
129100 }
130101}
0 commit comments