Skip to content

Commit d389f86

Browse files
committed
Make sure we only start as many threads as we need if n < t, properly this time
1 parent dcc7ac1 commit d389f86

File tree

1 file changed

+19
-48
lines changed

1 file changed

+19
-48
lines changed

src/main/java/net/dries007/tfc/seedmaker/CommandLineInterface.java

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)