Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will parameter of a static object's non-static method be shared from multiple threads?

(All of the below are simplified for brevity)

I have a static reference to a processing object.

The Processor contains a function to which I can pass an object Package, which is the info to be processed.

The Package comes from a list of lots of packages which should be processed. These packages are unqique; there are not multiple references to the same object here.

I start many threads so that I can process the packages in toProcess in parallell. The threads Pop() an available Package from the toProcess stack until the stack is empty.

My question is now: Can I risk that package in Check is overwritten by another thread, or will the other thread "create a copy" of Check?

static class Program
{
    static Processor processor = new Processor();
    static Stack<Package> toProcess;
    static Object sync = new Object();

    static void RunThreads()
    {
        toProcess = GetPackages();
        //start many threads using the Work delegate
    }

    static void Work()
    {
        while(true)
        {
            Package p;
            lock(sync)
            {
                if(toProcess.Count == 0)
                   break;
                p = toProcess.Pop();
            }
            processor.Check(p);
        }
    }
}

class Processor
{
    public void Check(Package package)
    {
        //do many analyses on p and report results of p's analysis to elsewhere
    }
}

class Package
{
    //properties
}
like image 935
David S. Avatar asked Sep 07 '12 16:09

David S.


People also ask

Can static method be accessed by multiple threads?

accessing the code is no problem, static methods can be called with multiple threads. It depends on how it is programmed in the method, if the code is not thread safe, it will cause problems.

Can two threads execute static and non-static method concurrently?

Since both the objects are different hence both synchronized static and non-static method will not block each other in case of multi-threading. Both the methods will execute simultaneously. Save this answer. Show activity on this post.

Is static class shared between threads?

A static field marked with ThreadStaticAttribute is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field.

Are static variables shared between threads C?

Each thread will share the same static variable which is mostly likely a global variable. The scenario where some threads can have wrong value is the race condition (increment isn't done in one single execution rather it is done in 3 assembly instructions, load, increment, store).


2 Answers

My question is now: Can I risk that package in Check is overwritten by another thread, or will the other thread "create a copy" of Check?

It's not really clear what you mean, but each independent call to Check will have its own package variable. That variable is local to not just that thread, but that invocation of Check (so recursive calls won't change it either).

Now if you pass the same Package reference to Check in multiple different threads, then yes, they will all be processing the same object, which may cause problems. But the package variables themselves will be independent in the different calls.

like image 177
Jon Skeet Avatar answered Oct 18 '22 06:10

Jon Skeet


If Package is a class, i.e. a reference type, and if in Check you execute some write operation, yes - you will have to manage access to it with locking mechanism.

On the other hand: if Package is a value type, it will be passed to the function by value. If Check only reads information (even if Package is a reference type), then there is no need to worry about locking.

like image 2
Tigran Avatar answered Oct 18 '22 07:10

Tigran