using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication6
{
class producerconsumer
{
static int buf;
static void Main(string[] args)
{
buf = 0;
Thread t1 = new Thread(consumer);
Thread t2 = new Thread(producer);
t1.Start();
t2.Start();
}
static void consumer()
{
for (int x = 1; x < 5000; )
if (buf > 0)
{
buf--;
Thread.Sleep(2000);
Console.WriteLine("Consumer" + x++);
}
else
{
Console.WriteLine("Consumer Waiting");
Thread.Sleep(1000);
}
}
static void producer()
{
for (int x = 1; x < 5000; )
if (buf < 4)
{
buf++;
Thread.Sleep(1000);
Console.WriteLine("producer" + x++);
}
else
{
Console.WriteLine("Producer waiting");
Thread.Sleep(1000);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication6
{
class producerconsumer
{
static int buf;
static void Main(string[] args)
{
buf = 0;
Thread t1 = new Thread(consumer);
Thread t2 = new Thread(producer);
t1.Start();
t2.Start();
}
static void consumer()
{
for (int x = 1; x < 5000; )
if (buf > 0)
{
buf--;
Thread.Sleep(2000);
Console.WriteLine("Consumer" + x++);
}
else
{
Console.WriteLine("Consumer Waiting");
Thread.Sleep(1000);
}
}
static void producer()
{
for (int x = 1; x < 5000; )
if (buf < 4)
{
buf++;
Thread.Sleep(1000);
Console.WriteLine("producer" + x++);
}
else
{
Console.WriteLine("Producer waiting");
Thread.Sleep(1000);
}
}
}
}
No comments:
Post a Comment