如何在我的程序中添加另一个消费者?我试着这个,但没有工作...... 我发现这篇文章how to add consumers in multithreading,但它不是我需要的,因为我不需要锁......我需要没有课或类似的东西。只是我的代码..请帮助:))
private int occupiedBufferCount = 0;
private int occupiedBufferCount2 = 0;
int i = 0;
private void Producer()
{
int how_much_numbers = Convert.ToInt32(textBox3.Text);
using (StreamWriter writer = new StreamWriter("random_skaiciai.txt"))
{
for (i = 0; i < how_much_numbers; i++)
{
Monitor.Enter(this);
if ((occupiedBufferCount == 1) || (occupiedBufferCount2 == 1))
{
Monitor.Wait(this);
}
++occupiedBufferCount;
buffer = i;
Random rnd = new Random();
numbers = i;
//numbers = rnd.Next(nuo, iki);
writer.WriteLine(numbers + "");
prm = false;
fib = false;
Monitor.Pulse(this);
Monitor.Exit(this);
if (isCanceled == true)
break;
}
writer.Close();
Set_p(kiek);
}
}
private void Consumer1()
{
int how_much_numbers = Convert.ToInt32(textBox3.Text);
using (StreamWriter writer = new StreamWriter("Primary_numbers.txt"))
{
while (i < how_much_numbers)
{
Monitor.Enter(this);
if ((occupiedBufferCount == 0))
{
Monitor.Wait(this);
}
--occupiedBufferCount;
if (numbers != 0)
if (prime_num(numbers) == true)
{
writer.WriteLine(numbers + "");
}
prm = true;
Monitor.Pulse(this);
Monitor.Exit(this);
if (isCanceled == true)
break;
}
writer.Close();
}
}
private void Consumer2()
{
int how_much_numbers = Convert.ToInt32(textBox3.Text);
using (StreamWriter writer = new StreamWriter("fibon_Numbers.txt"))
{
while (i < how_much_numbers)
{
Monitor.Enter(this);
if ((occupiedBufferCount2 == 0))
{
Monitor.Wait(this);
}
--occupiedBufferCount2;
if (numbers != 0)
if (isfibonaci(numbers) == true)
writer.WriteLine(numbers + "");
Monitor.Pulse(this);
Monitor.Exit(this);
if (isCanceled == true)
break;
}
writer.Close();
}
}