Showing posts with label Thread Synchronization. Show all posts
Showing posts with label Thread Synchronization. Show all posts

Thread Synchronization in C#

 Thread Synchronization in C#



using System;

using System.Collections;

using System.Numerics;

using System.Threading;

using System.Threading.Tasks;


namespace CSharp

{

    public class Q

    {

    Queue q = new Queue();


        public void put(int n)

        {

            lock (this)

            {

                while(q.Count==1)

                {

                    Monitor.Wait(this);

                }

                q.Enqueue(n);            

                Monitor.Pulse(this);

            }

        }


        public int get()

        {

            lock (this)

            {

                while (q.Count==0)

                {                

                    Monitor.Wait(this);

                }

                int n = (int)q.Dequeue();

                Monitor.Pulse(this);

                return n;

            }

        }

    }


    public class Consumer 

    {

        Q q;


        public Consumer(Q q)

        {

            this.q = q;

            new Thread(new ThreadStart(this.Consume)).Start();

        }



        public void Consume()

        {

            for (int i=1; i <= 5; i++)

            {

                int n = q.get();

                Console.WriteLine ("Consuming {0}", n);

                Thread.Sleep(200);

            }

        }

    }

    

    public class Producer 

    {

        Q q;


        public Producer(Q q)

        {

            this.q = q;

            new Thread(new ThreadStart(this.Produce)).Start();

        }



        public void Produce()

        {

            for (int i=1; i <= 5; i++)

            {

                q.put(i);

                Console.WriteLine ("Producing {0}", i);

                Thread.Sleep(200);

            }

        }

    }

    

    public class Program

    {

        public static void Main(string[] args)

        {

            Q q = new Q();

            

            new Producer(q);

            new Consumer(q); 

        }

    }

}


Java Threads - Producer consumer problem

// Thread Synchronization
// Producer Consumer Problem

class Q
{
     int n;
     boolean Flag = false;

     synchronized int get()
     {
          if(Flag == false)
          try
          {
              wait();
          }
          catch(InterruptedException e)
          {
              System.out.println("InterruptedException caught");
          }
         
          System.out.println("Received : "+n);
          Flag = false;
          notify();
          return n;
     }  

     synchronized void put(int n)
     {
          if(Flag == true)
          try
          {
              wait();
          }
          catch(InterruptedException e)
          {
              System.out.println("InterruptedException caught");
          }
         
          this.n = n;
          Flag = true;
          System.out.println("Sent : "+n);
          notify();
     }  
}

class Producer implements Runnable
{
     Q q;
     Thread t;
    
     Producer(String name, Q q)
     {
          this.q = q;
          t = new Thread(this, name);
          t.start();
     }

     public void run()
     {
          int i=1;
         
          while(true)
              q.put(i++);
     }
}


class Consumer extends Thread
{
     Q q;
    
     Consumer(String name, Q q)
     {
          super(name);
          this.start();
          this.q = q;
     }

     public void run()
     {
          while(true)
              q.get();
     }
}

class PC
{
     public static void main(String as[])
     {
          Q q1 = new Q();
          Producer p1 = new Producer("P", q1);
          Consumer c1 = new Consumer("C", q1);
     }
}