i'm developing an app which download map tiles around different places in a city. To do this, i have one thread for each place in which i select the tiles and create a thread to download each.
Well, the question is how to a开发者_如何学Cvoid creating a thread for a tile that already exists in the thread pool.
Should not just check if the file exists, since it is possible that the thread for that tile already exists (other place already need that tile) but the file has not been created-
Any idea? Thanks
You can use a ConcurrentHashMap<Tile, Thread>
in which you store all the downloader threads.
You add a thread to the hashmap before starting it and you remove it before destroying it. Of course you will check the hashmap for a specific Tile
before starting a thread for that tile.
This is the simplest solution that comes into my mind, it would be easy to implement but you'll have to read carefully how to use this kind of hashmap to have guaranteed concurrency with no consistency problems..
You can also just take the "set" feature of the hashmap by using
Collections.newSetFromMap(new ConcurrentHashMap<Tile,Thread>())
In this way you won't have any association Tile
->Thread
but just a structure for which you can do a set.contains(tile)
to check if a tile has already been scheduled by some thread.
A better design might be to have a single ExecutorService that does the downloading. It has access to a BlockingQueue that it reads from to know which tiles to download. Elsewhere in your application, tiles would be placed onto this queue.
精彩评论