Skip to content

Commit 9a73fe5

Browse files
committed
add examples on deffered operation and manual staging
1 parent c3a9295 commit 9a73fe5

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.elebras1.flecs.examples;
2+
3+
import com.github.elebras1.flecs.Entity;
4+
import com.github.elebras1.flecs.Query;
5+
import com.github.elebras1.flecs.World;
6+
import com.github.elebras1.flecs.examples.components.Health;
7+
8+
public class DeferredOperationsExample {
9+
10+
public static void main(String[] args) {
11+
try(World world = new World()) {
12+
world.component(Health.class);
13+
14+
for(int i = 0; i < 1000; i++) {
15+
Entity entity = world.obtainEntity(world.entity("entity_" + i));
16+
entity.set(new Health(i));
17+
}
18+
19+
Query query = world.query().with(Health.class).build();
20+
21+
world.deferBegin();
22+
for(int i = 10; i < 20; i++) {
23+
long entityId = world.lookup("entity_" + i);
24+
Entity entity = world.obtainEntity(entityId);
25+
entity.remove(Health.class);
26+
}
27+
if(query.count() != 1000) {
28+
throw new RuntimeException("Expected 1000 entities, but got " + query.count());
29+
}
30+
world.deferEnd();
31+
32+
if(query.count() != 990) {
33+
throw new RuntimeException("Expected 990 entities, but got " + query.count());
34+
}
35+
36+
query.close();
37+
}
38+
}
39+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.github.elebras1.flecs.examples;
2+
3+
import com.github.elebras1.flecs.Entity;
4+
import com.github.elebras1.flecs.Query;
5+
import com.github.elebras1.flecs.World;
6+
import com.github.elebras1.flecs.examples.components.Health;
7+
8+
import java.lang.foreign.MemorySegment;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.concurrent.ExecutionException;
12+
import java.util.concurrent.ExecutorService;
13+
import java.util.concurrent.Executors;
14+
import java.util.concurrent.Future;
15+
16+
import static jdk.internal.jrtfs.JrtFileAttributeView.AttrID.size;
17+
18+
public class ManualStagingExample {
19+
private static final int THREADS = 4;
20+
21+
public static void main(String[] args) {
22+
ExecutorService executor = Executors.newFixedThreadPool(THREADS);
23+
List<Future<?>> futures = new ArrayList<>();
24+
25+
try (World world = new World()) {
26+
world.setStageCount(THREADS);
27+
world.component(Health.class);
28+
29+
for (int i = 0; i < 1000; i++) {
30+
Entity entity = world.obtainEntity(world.entity("entity_" + i));
31+
entity.set(new Health(i));
32+
}
33+
34+
for (int i = 0; i < THREADS; i++) {
35+
final int stageId = i;
36+
futures.add(executor.submit(() -> {
37+
World stage = world.getStage(stageId);
38+
for(int j = stageId * 250; j < (stageId + 1) * 250; j++) {
39+
long entityId = world.lookup("entity_" + j);
40+
Entity entity = stage.obtainEntity(entityId);
41+
entity.set(new Health(j));
42+
}
43+
}));
44+
}
45+
46+
for (Future<?> f : futures) {
47+
f.get();
48+
}
49+
50+
world.merge();
51+
executor.shutdown();
52+
53+
} catch (ExecutionException | InterruptedException e) {
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)