I'm very new to multithreading - I've done a few hours of research and tested a few different ways. I've recently implemented this code:
public class resetDataThread implements Runnable {
VertexHashMap allVertices;
public resetDataThread(VertexHashMap allVertices){
this.allVertices = allVertices;
}
@Override
public void run() {
for (int i = 0; i < allVertices.data.length; i++)
{
if (allVertices.data[i] != null){
allVertices.data[i].inClosed = false;
allVertices.data[i].inOpen = false;
}
}
System.out.println("Thread Finished!");
}
}
Please note, VertexHashMap is a self implemented hash map that stores Vertices.
Runnable r = new resetDataThread(allVertices);
Thread t1 = new Thread(r);
I call the method like so:
t1.run();
This line of code gets called many times without creating new instances of r or t1. My reason for implementing this is so my program can fetch new data and output results while it resets the storage's boolean variables (allVertices). My main aim is execution speed of my program, so I figured I could multi thread this section of code.
So my question is, is this safe, suitable or a good way to do this?
Example
...code that changes allVertices variables
t1.run(); //this will run while otherSectionOfProgram and yetAnotherSectionOfProgram is executed
otherSectionOfProgram();
yetAnotherSectionOfProgram();
The example above can be called many times however the contents of allVertices will always be the same (except for the boolean settings), so I'm just wanting to make sure it's okay to use the run() method the way I have - as I want to ensure I practise good java/programming.