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
}

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.


Need Your Help

Could not retrieve contact Presence using aSmack library for connecting google talk service

android xmpp contacts user-presence

I'm trying to implement a chat client for Android which connects to google talk service. I have done most of it, I connect to the server, I get contacts (and show them in a ListView) and I open a new

Getting minimum with a default

ruby

In my params hash, I receive a value for :limit. I need to assign this value to a variable limit, but not exceed 50, and if the value for :limit is not given, default to 50.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.