10
10
import java .util .concurrent .BlockingQueue ;
11
11
import java .util .concurrent .CountDownLatch ;
12
12
13
+ /**
14
+ * This class implements a basic TaskSystem in java, for multithreading of a box2d world.
15
+ * One {@link Box2dWorldMultiThreader} handles one world.
16
+ * The MultiThreader will create and keep alive the configured amount of workers,
17
+ * After every world step, {@link Box2dWorldMultiThreader#afterStep()} needs to be called.
18
+ * After the world is destroyed, {@link Box2dWorldMultiThreader#dispose()} needs to be called.
19
+ * This class, like box2d, is not thread safe.
20
+ */
13
21
public class Box2dWorldMultiThreader implements Disposable {
14
22
15
23
private static final int MAX_TASKS = 128 ;
@@ -24,6 +32,15 @@ public class Box2dWorldMultiThreader implements Disposable {
24
32
private int taskCount = 0 ;
25
33
private boolean running = true ;
26
34
35
+ /**
36
+ * This method creates and configures a {@link b2WorldDef} to use a newly created {@link Box2dWorldMultiThreader}
37
+ *
38
+ * After the world is destroyed, {@link Box2dWorldMultiThreader#dispose()} needs to be called.
39
+ *
40
+ * @param worldDef The world to configure
41
+ * @param numWorkers The amount of worker
42
+ * @return The created {@link Box2dWorldMultiThreader}
43
+ */
27
44
public static Box2dWorldMultiThreader createForWorld (b2WorldDef worldDef , int numWorkers ) {
28
45
Box2dWorldMultiThreader multiThreader = new Box2dWorldMultiThreader (numWorkers );
29
46
multiThreader .configureForWorld (worldDef );
@@ -35,7 +52,7 @@ private Box2dWorldMultiThreader(int numWorkers) {
35
52
throw new IllegalArgumentException ("Number of workers must be greater than 1" );
36
53
this .numWorkers = numWorkers ;
37
54
this .workers = new Thread [numWorkers ];
38
- taskChunks = new ArrayBlockingQueue <>(MAX_TASKS * numWorkers );
55
+ this . taskChunks = new ArrayBlockingQueue <>(MAX_TASKS * numWorkers );
39
56
40
57
for (int i = 0 ; i < numWorkers ; i ++) {
41
58
final int workerId = i ;
@@ -106,11 +123,17 @@ private void configureForWorld(b2WorldDef worldDef) {
106
123
worldDef .workerCount (numWorkers );
107
124
}
108
125
126
+ /**
127
+ * This method needs to be called after every world step
128
+ */
109
129
public void afterStep () {
110
130
taskCount = 0 ;
111
131
}
112
132
113
133
134
+ /**
135
+ * Cleans up all resources. Should not be called before destroying the world.
136
+ */
114
137
@ Override
115
138
public void dispose () {
116
139
running = false ;
0 commit comments