I have a school assignment where I have to multiple threads drawing rectangles over an image. Shapes in progress cannot overlap (ie two threads cannot be simultaneously trying to draw in the same region) I have to do this using get/setRGB but I'm trying to wrap my head around providing that exclusion. I'm thinking that the critical resource would be the pixels in each region so how would i have each thread lock the individual pixels?
-
1What have you tried so far? Post your code, please. – akuzminykh Jan 26 '20 at 20:00
-
Would like to avoid posting code for class rules but single thread logic is a thread basically iterates through array of shape size on image; filling colors as necessary for some determined dimensions and coords. For multiple threads, I'm thinking i have to provide mutual exclusion for coord regions but I'm at a loss at how. (maybe there is a better way?) – spacecadetmatt Jan 26 '20 at 20:18
-
Your question is not very specific. It's hard to help you like that without specific code describing your specific problem. If you don't like to post some code, I'd like to link you some tools that could help you: [z buffer algorithm](https://www.geeksforgeeks.org/z-buffer-depth-buffer-method/), [AtomicInteger](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html), [synchronized](https://www.baeldung.com/java-synchronized). – akuzminykh Jan 26 '20 at 20:51
-
Use a shared resource that is shared between all threads and that contains the last pixel coordinates visited by a thread. Any upcoming threads that are trying to draw in the shared resource coordinates can be redirected to draw somewhere else. – nasdenkov Jan 26 '20 at 21:50
1 Answers
You need to have one SpaceManager, a centralized agent which will manage doling out the rectangle regions for a given BufferedImage. This will need an allocateRectangle(int sizeX, int sizeY) method which will find unused space, tag it as used, and return a DrawingRectangle, and a freeRectangle(DrawingRectangle dr) method which will free up the space when it is no longer being used.
The DrawingRectangle is your own class which has a reference to the BufferedImage and it knows its own offset within it. It has its own get/setRGB methods which just add its X and Y offsets and calls the BufferedImage get/setRGB. So the clients of this system just get a DrawingRectangle and just access it as if it were its own BufferedImage with the size they requested.
The trick here is that your allocateRectangle and freeRectangle methods must be synchronized, so that the process of allocating one doesn't get interrupted by another thread which then tries to allocate its own. Managing 2-D space is tricky if you're trying to optimize the packing, but I wouldn't bother, at least at first. Just think of it as rows were rectangles are aligned on the top. If the next new one won't fit in an existing row, make a new row that starts just below the tallest Rectangle of the lowest row.

- 638
- 4
- 8