תרגיל קטן שאני מנסה ב multithreading ולא מבין למה יוצא לי שטויות - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

תרגיל קטן שאני מנסה ב multithreading ולא מבין למה יוצא לי שטויות


yigael_o

Recommended Posts

אני מנסה ליצור תוכנית שתחשב FACTORIAL.

אני יודע שהתוכנית לא מתאימה במקרי קצה אבל מבחינתי אני רק רוצה לבדוק שהיא עובדת כאשר צריך לחשב 10 ב 2 נימים, ומקרה נוסף של 100 ב 4 נימים.

מסיבה כלשהיא יוצא לי תוצאות זבל, שלא לדבר על כך שבריצה ודיבוג התוצאות שונות לחלוטין.



using System;

using System.Collections.Generic;

using System.Threading;

namespace Factorial

{

static class Program

{

public static double TotalProduct = 1;

public static int NumberOfThreadsThatFinished;



static void Main()

{

//get number to check the facturial on

Console.WriteLine("Please enter the number to calculate the facturial on: ");

int number = int.Parse(Console.ReadLine());

//get the number of threads, this number should be more then 2, to keep this program simple we will NOT check the input for validity

Console.WriteLine("Please enter the number of threads to run: ");

int threadsNumber = int.Parse(Console.ReadLine());

//create a list to hold all the threads

var threadList = new List<Thread>();

for (int i = 0; i < threadsNumber; i++)

{

PartFactorial partFactorial=new PartFactorial();

threadList.Add(new Thread(partFactorial.CalculateTheProductFromNumberToMunb er));

}

//run all threads, but create a variable first to hold the data to send to the threads

var twoNumbers = new TwoNumbers {FirstNumber = 1};

int jumpSize = number / threadsNumber; //more check should be here to prepare to all situations, lets just expect that the inputed data was normal.

twoNumbers.LastNumber = jumpSize;

for (int i = 0; i < threadsNumber; i++)

{

Console.WriteLine("The number smalest is: {0}, the biggest number is: {1}", twoNumbers.FirstNumber, twoNumbers.LastNumber);

threadList[i].Start(twoNumbers);

twoNumbers.FirstNumber = twoNumbers.LastNumber + 1;

if ((i + 2) < threadsNumber)

{

twoNumbers.LastNumber += jumpSize;

}

else

{

twoNumbers.LastNumber = number;

}

}

Console.WriteLine();

while (NumberOfThreadsThatFinished < threadsNumber)

{

Console.Write(".");

}

Console.WriteLine("The product is {0}", TotalProduct);

Console.ReadLine();

}

}

//class to hold the input to the function, the first number and the last number

public class TwoNumbers

{

public int FirstNumber { get; set; }

public int LastNumber { get; set; }

}

}


והקובץ השני:

using System;

namespace Factorial

{

class PartFactorial

{

private static readonly object ConsoleObj = new object();

private static readonly object SyncObjProduct = new object();

private static readonly object SyncObjThread = new object();

public void CalculateTheProductFromNumberToMunber(object input)

{

var twoNumbers = (TwoNumbers)input;

//holds the product

int product = 1;

for (int i = twoNumbers.FirstNumber; i <= twoNumbers.LastNumber; i++)

{

product *= i;

lock (ConsoleObj)

{

Console.WriteLine("Temp product: {0}", product);

}

}

lock (SyncObjProduct)

{

Program.TotalProduct *= product;

Console.WriteLine("Final product********* {0}", product);

lock (SyncObjThread)

{

Program.NumberOfThreadsThatFinished += 1;

}

}

}

}

}

[attachment deleted by admin]

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...