使用多个线程添加两个数组 [英] Adding two arrays using multiple threads

查看:55
本文介绍了使用多个线程添加两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个问题,我没有得到数组C中某些数组元素的结果?

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Threading;
命名空间 Finding_the_sum_of_two_arrays
{
class 程序
{
public static int [ ] a = new int [ 100 ] ;
public static int [ ] b = new int [ 100 ] ;
public static int [ ] c = new int [a.Length];
public static int numberofthreads ;
public static bool thread_lock = false ;

静态 void Main( string [] args)
{
Random r = new Random();
for int i = 0 ; i < 100 ; i ++)
{
a [i] = r.Next( 5 20 );
b [i] = r.Next( 10 30 );
}
Console.WriteLine( 输入要使用的线程数:);
numberofthreads = int .Parse(Console.ReadLine());


Thread [] threadsarray = new Thread [numberofthreads];


for int i = 0 ; i < numberofthreads; i ++)
{
threadsarray [i] = new 线程( new ParameterizedThreadStart(myThreadMethod));
threadsarray [i] .Start(i);
threadsarray [i] .Join();
}
Console.WriteLine( 第一个数组:);
Console.WriteLine( string .Join( ,a));
Console.WriteLine( Secound Array:);
Console.WriteLine( string .Join( ,b));
Console.WriteLine( 结果数组:);
Console.WriteLine( string .Join( ,c));
Console.WriteLine( );
}
静态 void myThreadMethod( object threadid)
{
int start = a.Length;
int thid =( int )threadid;
while (!thread_lock)
thread_lock = true ;

for int i = thid * a.Length / numberofthreads; i < thid + 1 * a.Length / numberofthreads; i ++)
{
c [i] = a [i] + b [i];

}

thread_lock = false ;

}

}
}

解决方案

原因你问的问题纯粹是数学问题:你计算 i 的起始值和结束值是不正确的。使用3个线程, 中的 -loop在 myThreadMethod(..)中迭代这些范围:



线程0:0-32

线程1:33-33

线程2:无(初始化为66,但退出条件立即评估为真并中止循环)



我假设你将能够现在你已经意识到了这一点。



但还有另一个问题:你很可能打算将这个程序改为多线程,但事实并非如此。你的线程是同步运行的,因为你立即用主线程.Join()它们。你需要在开始所有线程之后加入它们。



你应该学会使用调试器! :)


问题通过改变for循环从

获得(int i = thid * a.Length / numberofthreads;我< thid + 1 * a.Length / numberofthreads; i ++)





to



< pre> for(int i = thid * a.Length / numberofthreads; i<(thid + 1)* a.Length / numberofthreads; i ++)< / pre> 


I have an issue in my code where I do not get the result of some of the arrays elements in array C ?

using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.Threading;
   namespace Finding_the_sum_of_two_arrays
   {
    class Program
    {
        public static int[] a = new int[100];
        public static int[] b = new int[100];
        public static int[] c = new int[a.Length];
        public static int numberofthreads;
        public static bool thread_lock = false;

        static void Main(string[] args)
        {
            Random r = new Random();
            for (int i = 0; i < 100; i++)
            {
                a[i] = r.Next(5, 20);
                b[i] = r.Next(10, 30);
            }
            Console.WriteLine("Enter the Number of threads to use: ");
            numberofthreads = int.Parse(Console.ReadLine());


            Thread[] threadsarray = new Thread[numberofthreads];


            for (int i = 0; i < numberofthreads; i++)
            {
                threadsarray[i] = new Thread(new ParameterizedThreadStart(myThreadMethod));
                threadsarray[i].Start(i);
                threadsarray[i].Join();
            }
            Console.WriteLine("The First Array : ");
            Console.WriteLine(string.Join(",", a));
            Console.WriteLine("The Secound Array : ");
            Console.WriteLine(string.Join(",", b));
            Console.WriteLine("The Result Array : ");
            Console.WriteLine(string.Join(",", c));
            Console.WriteLine(" ");
            }
        static void myThreadMethod(object threadid)
        {
            int start = a.Length;
            int thid = (int)threadid;
            while (!thread_lock)
                thread_lock = true;
       
            for (int i = thid * a.Length / numberofthreads; i < thid + 1 * a.Length / numberofthreads; i++)
            {
                c[i] = a[i] + b[i];
               
            }
        
            thread_lock = false;

        }

    }
}

解决方案

The cause for the problem you're asking about is purely mathematical: Your calculation of the start- and end-values for i is incorrect. With 3 threads, the for-loop in myThreadMethod(..) iterates over these ranges:

thread 0: 0-32
thread 1: 33-33
thread 2: none (i gets initialized with 66 but the exit-condition immediately evaluates to true and aborts the loop)

I assume you'll be able to fix this now that you're aware of it.

But there's another problem: You most probably intended this program to be multi-threaded but it isn't. Your threads run synchronously because you immediately .Join() them with the main thread. You need to join them not earlier than after you started all threads.

You should learn to use the debugger! :)


The Problem was solved by changing the for loop from

for (int i = thid * a.Length / numberofthreads; i< thid + 1 * a.Length / numberofthreads; i++)



to

<pre>for (int i = thid * a.Length / numberofthreads; i <(thid + 1) * a.Length / numberofthreads; i++)</pre>


这篇关于使用多个线程添加两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆