How does static code run with multiple threads?
        Posted  
        
            by Krisc
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Krisc
        
        
        
        Published on 2010-03-26T15:43:12Z
        Indexed on 
            2010/03/26
            15:53 UTC
        
        
        Read the original article
        Hit count: 347
        
I was reading http://stackoverflow.com/questions/1511798/threading-from-within-a-class-with-static-and-non-static-methods and I am in a similar situation.
I have a static method that pulls data from a resource and creates some runtime objects based on the data.
static class Worker{
    public static MyObject DoWork(string filename){
        MyObject mo = new MyObject();
        // ... does some work
        return mo;
    }
}
The method takes awhile (in this case it is reading 5-10mb files) and returns an object.
I want to take this method and use it in a multiple thread situation so I can read multiple files at once. Design issues / guidelines aside, how would multiple threads access this code?
Let's say I have something like this...
class ThreadedWorker {
    public void Run() {
        Thread t = new Thread(OnRun);
        t.Start();
    }
    void OnRun() {
        MyObject mo = Worker.DoWork("somefilename");
        mo.WriteToConsole();
    }
}
Does the static method run for each thread, allowing for parallel execution?
© Stack Overflow or respective owner