|
| 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