Will a static object parameter of a non-static method from multiple threads be used?

(All of the following are simplified for brevity)

I have a static reference to a processing object.

The processor contains a function with which I can pass a Package object, which is the information being processed.

A package comes from a list of packages that need to be processed. These packages are fuzzy; there are no multiple references to the same object.

I run many threads so that I can process packages in toProcess in parallell. In Pop () streams, a package from the toProcess stack is available until the stack is empty.

Now my question: Can I take a chance that packagein Checkwill be overwritten by another thread or another thread "creates a copy" 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 analysis to elsewhere
    }
}

class Package
{
    //properties
}
+5
3

: , Check , " " Check?

, , Check package. , Check ( ).

, package Check , , , . package .

+3

Package class, , Check , , .

: Package - , . Check ( Package ), .

+2

, , , .

Check, Processor.

0

All Articles