Saturday 16 April 2016

c# thread workout 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace threadlocks
{
    class stuff
    {
        public void addup()
        {
            lock (Program.obj) //vb  synclock
            {
                for (long x = 1; x <= 50000000; x = x + 1)
                    Program.total = Program.total + 1;
                Console.WriteLine(Thread.CurrentThread.Name + "         " + Program.total + "      active thread #         " + Program.active_thread_number);
                Program.active_thread_number--;
                if (Thread.CurrentThread.Name == "9")
                {
                    Program.done = true;
                }
            }
        }

        public void hello()
        {
            Console.WriteLine(Thread.CurrentThread.Name + "     hello  ");
        }
    }
    class Program
    {
        public static long total = 0;
        public static object obj = 0;
        public static int active_thread_number=0;
        public static Thread[] t = new Thread[10];
        public static bool done = false;

        static void Main(string[] args)
        {
            stuff s = new stuff();
           for (int i = 0; i < 10; i++)
            {
                t[i] = new Thread(new ThreadStart(s.addup));
                t[i].Name = i.ToString();              
                //t.Start();              
            }

           //t[0].Start();

           for (int i = 0; i < 10; i++)
           {
              /* while (t[i - 1].IsAlive)
               {
               }*/
               while (active_thread_number >= 3)
               {
               }
               t[i].Start();
               Console.WriteLine("thread    " + i + "   started");
               active_thread_number++;
           }

            Thread x = new Thread(new ThreadStart(s.hello));
            x.Name = "x";
            x.Start();
         
            while (!done)
            {
            }
            Console.WriteLine(total);
           
            Console.ReadLine();
        }
    }
}

-------------------------------------------------------------------------------

thread    0   started
thread    1   started
thread    2   started
0         50000000      active thread #         3
thread    3   started
1         100000000      active thread #         3
thread    4   started
2         150000000      active thread #         3
thread    5   started
3         200000000      active thread #         3
thread    6   started
4         250000000      active thread #         3
thread    7   started
5         300000000      active thread #         3
thread    8   started
6         350000000      active thread #         3
thread    9   started
x     hello
7         400000000      active thread #         3
8         450000000      active thread #         2
9         500000000      active thread #         1
500000000

1 comment:

  1. Want To Boost Your ClickBank Traffic And Commissions?

    Bannerizer made it easy for you to promote ClickBank products by banners, simply visit Bannerizer, and get the banner codes for your favorite ClickBank products or use the Universal ClickBank Banner Rotator Tool to promote all of the ClickBank products.

    ReplyDelete