Skip to content
Snippets Groups Projects
Select Git revision
  • 2ed1eea32c8e365c2156df125b8a67de21f1061f
  • master default protected
  • development
  • dev
4 results

SharedRessources.java

Blame
  • SharedRessources.java 2.46 KiB
    package approach3.parallel;
    
    import structs.Int3;
    
    import java.awt.*;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class SharedRessources {
        private static final int AVAIBLE_CORES
                = Runtime.getRuntime().availableProcessors();
    
        private static int MAX_CORES = 1;
    
        public static void setMaxCores(int cores) {
            MAX_CORES = cores >= AVAIBLE_CORES ? AVAIBLE_CORES : cores;
        }
    
        private static SharedRessources sharedRessources;
    
        public static SharedRessources getInstance() {
            if (sharedRessources == null)
                sharedRessources
                        = new SharedRessources();
    
            return sharedRessources;
        }
    
        private ExecutorService threadPool;
    
        private Int3[] data;
        private boolean[] walls;
    
        private int width;
        private int height;
    
        private Point start;
        private Point end;
    
        private volatile boolean found;
    
        private SharedRessources() {
            threadPool = Executors.newFixedThreadPool(MAX_CORES);
        }
    
        public void initialize(int w, int h) {
            this.width = w;
            this.height = h;
    
    
            data = new Int3[width * height];
            for (int i = 0; i < data.length; i++)
                data[i] = new Int3(0, 0, Integer.MAX_VALUE);
    
            walls = new boolean[width * height];
        }
    
    
        public void setFound(boolean found) {
            this.found = found;
        }
    
        public boolean isFound() {
            return found;
        }
    
        public void setStart(int x, int y) {
            start = new Point(x, y);
        }
    
        public void setEnd(int x, int y) {
            end = new Point(x, y);
        }
    
        public Point getStart() {
            return start;
        }
    
        public Point getEnd() {
            return end;
        }
    
        public ExecutorService getExecutor() {
            return threadPool;
        }
    
        public int getStep(int x, int y) {
            synchronized (this) {
                return data[x + y * width].getZ();
            }
        }
    
        public void setData(int x, int y, int dirX, int dirY, int step) {
            synchronized (this) {
                data[x + y * width] = new Int3(dirX, dirY, step);
            }
        }
    
        public Int3 getData(int x, int y) {
            return data[x + y * width];
        }
    
        public boolean isWall(int x, int y) {
            return walls[x + y * width];
        }
    
        public void setWall(int x, int y) {
            walls[x + y * width] = true;
        }
    
        public int getWidth() {
            return this.width;
        }
    
        public int getHeight() {
            return this.height;
        }
    
    }