相关疑难解决方法(0)

C#数组是否安全?

特别是

  1. 创建一个函数以将数组和索引作为参数.
  2. 创建一个元素数组.
  3. 创建一个计数循环.
  4. 在新线程的循环内部,使用传入的索引器将对象的新实例分配给数组.

我知道如何管理线程等.我很想知道这是否是线程安全的做事方式.

 class Program
{
    // bogus object
    class SomeObject
    {
        private int value1;
        private int value2;

        public SomeObject(int value1, int value2)
        {
            this.value1 = value1;
            this.value2 = value2;
        }
    }

    static void Main(string[] args)
    {

        var s = new SomeObject[10];
        var threads = Environment.ProcessorCount - 1;
        var stp = new SmartThreadPool(1000, threads, threads);
        for (var i = 0; i < 10; i++)
        {
            stp.QueueWorkItem(CreateElement, s, i);
        }

    }

    static void CreateElement(SomeObject[] s, int index)
    { …
Run Code Online (Sandbox Code Playgroud)

c# arrays multithreading thread-safety

51
推荐指数
4
解决办法
3万
查看次数

使用多线程访问相同的字符串(StringBuilder)

我的问题是,如果我有时在同一个字符串上使用多线程

字符串不会被替换.(我在记事本上写了这个,所以语法可能是

错误)

使用System.Thread ...其他的课程

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 …
Run Code Online (Sandbox Code Playgroud)

c# string stringbuilder

0
推荐指数
1
解决办法
5875
查看次数