Frage

Mein Problem ist, wenn ich Multi-Thread auf derselben Saite verwenden irgendwann

die Zeichenfolge nicht bekommen ersetzen. (Ich schrieb dies auf Notizblock so Syntax sein kann

falsch)

mit System.Thread ... Andere ofcourse

class ....
{
    private static StringBuild container = new StringBuilder();

    static void Main(...)
    {
    container.Append(Read From File(Kind of long));
    Thread thread1 = new Thread(Function1);
        Thread thread2 = new Thread(Function2);
    thread1.Start();
    thread2.Start();
    //Print out container
    }

    static void Function1
    {
    //Do calculation and stuff to get the Array for the foreach
    foreach (.......Long loop........)
    {
    container.Replace("this", "With this")
    }
    }
    //Same goes for function but replacing different things.
    static void Function2
    {
    //Do calculation and stuff to get the Array for the foreach
    foreach (.......Long loop........)
    {
    container.Replace("this", "With this")
    }
    }
}

Nun irgendwann ein Element ersetzt nicht erhalten. Also meine Lösung dieses Problems ruft container.Replace auf einem anderen

Methode und ein „Schloss“ tun, die Arbeit, aber es ist der richtige Weg?

private class ModiflyString
{
        public void Do(string x, string y)
            {
                lock (this)
                {
                    fileInput.Replace(x, y);
                }
            }
}
War es hilfreich?

Lösung

Sie sollten die Stringbuilder-Objekt sperren sich (innerhalb der Ersetzen-Funktionen):

lock (container)
{
   container.Replace("this", "With this");
}

oder erstellen Sie ein separates Sperrobjekt:

static object _stringLock = new object();

...

lock(stringLock)
{
    container.Replace("this", "With this");
}

Andere Tipps

Ihre Verriegelung funktioniert nicht, wenn Sie mehr als 1 ModifyString Objekt erstellen, und ich vermute, Sie tun.

Eine einfache Version:

   public void Do(string x, string y)
   {
      lock (fileInput)
      {
         fileInput.Replace(x, y);
      }
   }

Es kann besser sein, ein eigenes Objekt zu schaffen macht die Verriegelung auf, aber die oben zeigt das Prinzip besser. Alle konkurrierenden Threads auf demselben Objekt sperren sollten

Ein Standardansatz würde wie folgt aussehen:

private static StringBuild container = new StringBuilder();
private static object syncLock = new object();  // simple object, 1-1 with container

und dann können Sie (Gewinde-) sicher verwenden:

   lock(syncLock)
   {
       container.Replace(...);
   }

Das funktioniert gut, solange beide Threads die gleiche Instanz der ModifyString Klasse. Mit anderen Worten, dies funktionieren werden, weil die Sperre auf „die“ eine Sperre auf derselben Instanz sein muss:

class Blah
{
    private static StringBuild container = new StringBuilder();

    private static ModifyString modifyString = new ModifyString();

    static void Main(...)
    {
    container.Append(Read From File(Kind of long));
    Thread thread1 = new Thread(Function1);
        Thread thread2 = new Thread(Function2);
    thread1.Start();
    thread2.Start();
    //Print out container
    }

    static void Function1
    {       

        //Do calculation and stuff to get the Array for the foreach
        foreach (.......Long loop........)
        {
           modifyString.Do("this", "With this")
       }
    }
    //Same goes for function but replacing different things.
    static void Function2
    {
        //Do calculation and stuff to get the Array for the foreach
        foreach (.......Long loop........)
        {
            modifyString.Do("this", "With this")
        }
    }
}

Es wird nicht Arbeit, wenn Sie die unten tun, weil Schloss (diese) würde Sinn nicht funktioniert sie sind zwei separate Instanzen:

class Blah
{
    private static StringBuild container = new StringBuilder();

    static void Main(...)
    {
    container.Append(Read From File(Kind of long));
    Thread thread1 = new Thread(Function1);
        Thread thread2 = new Thread(Function2);
    thread1.Start();
    thread2.Start();
    //Print out container
    }

    static void Function1
    {
       ModifyString modifyString = new ModifyString();
       //Do calculation and stuff to get the Array for the foreach
       foreach (.......Long loop........)
       {
          modifyString.Do("this", "With this")
       }
    }
    //Same goes for function but replacing different things.
    static void Function2
    {
       ModifyString modifyString = new ModifyString();

       //Do calculation and stuff to get the Array for the foreach
        foreach (.......Long loop........)
        {
            modifyString.Do("this", "With this")
        }
    }
}

Einige Leute würden tatsächlich schaffen ein „Dummy“ Objekt, das Schloss führen an Stelle von „this“ (man kann nicht sperren auf der Saite, da sie ein Werttyp ist) verwendet wird.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top