My question is how does one determine if a thread has completed its tasks?
My problem is I have a loader screen which spawns threads to load various things, then waits for all of the threads to complete before moving on to the next screen. However, the next screen is missing some of what should have been loaded.
The threads I’m spawning are doing things like loading Textures, using Textures to create Materials, creating TerrainQuads, and printing out statements so I can see where a thread gets to. I’ve tried three approaches to determine when all of my tasks are done, but all three declare success prematurely.
The three approaches I’ve tried are:
check if ScheduledThreadPoolExecutor.getActiveCount() equals zero
extend ScheduledThreadPoolExecutor with my own class. Increment a count when a thread is submitted and decrement in afterExecute(). Return done is true when the count is zero.
When submitting a thread, keep its Future. Then in the update loop, move on when Future.isDone() returns true.
I’ve tried stepping through the debugger, but that throws off timing which sometimes causes everything to load correctly. So I don’t think I have an exception. Rather I wonder if a thread is being blocked and put back on the queue to be continued at a later time. Still, I would think all three approaches should be able to determine if a thread is completed vs pending.
I can try to post code if it’s needed, but I’d like to see if my understanding is correct first.
You probably only need one background thread to load the assets. The bottleneck will the the disk IO and throwing more threads probably won’t help that.
But to know if a thread is done, you can have each thread just call a method in your main game that says loaderThreadDone(theThreadThatFinished), then check if all of the threads have notified they are done by calling that method.
I’m lost of the principal page of forum. My question is this.
I’m trying to make an application with a tracking device (OptiTrack of trackingtools), the application is that the SDK using NaturalPoint make a laser pointer in a virtual environment.
Could you please give me an example where you can make this connection?
Thanks Sploreg! I think that looks like it’s working.
FYI I agree that the disk I/O effectively nullifies multiple threads. This is mostly an exercise for me to figure out multi-threading for when I really do need it. But you’re right, I should probably look at restructuring things.
So… after further investigation, turns out I am throwing exceptions. It seems to be on a call to getAssetManager(). This prompts 2 questions:
@valcaris said:
Thanks Sploreg! I think that looks like it's working.
FYI I agree that the disk I/O effectively nullifies multiple threads. This is mostly an exercise for me to figure out multi-threading for when I really do need it. But you're right, I should probably look at restructuring things.
So... after further investigation, turns out I am throwing exceptions. It seems to be on a call to getAssetManager(). This prompts 2 questions:
1- Is the AssetManager thread-safe?
2- Is there a way to "retry" threads?
1) yes.
2) no. You wrote the thread so it's doing whatever you told it to. Catch your exceptions and deal with them or let them bubble up and the thread will die.
@zzuegg said:
I am using a asset loader thread like this which allows me to resue the thread instead of generating a new on on each load
Peusocode since i am not at home:
[java]
public class AssetBackgroundLoader extends Runnable{
ConcurrentLinkedQueue<String> assetsToLoad
ConcurrentLinkedQueue<Spatial> assetsLoaded
If you use a blocking queue for your "assets to load" then you won't have to have all of that self-written blocking infrastructure (that is actually slightly incorrect anyway, technically).