Многопоточное приложение: проблема с параллельностью выполнения потоков - C#
Формулировка задачи:
Здравствуйте,прошу помощи у вас по следующему вопросу. у меня есть многопоточное приложение, но проблема в том, что почему-то потоки thread1,thread2,thread3 работают не параллельно. не могу разобраться с данной проблемой
полный код программы:
Архив с программой:
помогите пожалуйста
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace _6
- {
- public partial class Form1 : Form
- {
- uint max_ochered;
- int ludy_v_ocheredy, vsego_obsluzheno;
- delegate void Ochered(object name);
- Thread thread;
- Thread thread1;
- Thread thread2;
- Thread thread3;
- public Form1()
- {
- InitializeComponent();
- }
- private void Start_Click(object sender, EventArgs e)
- {
- try
- {
- max_ochered = uint.Parse(textBoxMax.Text);
- }
- catch
- {
- if (max_ochered <= 0)
- {
- MessageBox.Show("Ошибка", "Число должно быть положительным");
- }
- textBoxMax.Clear();
- }
- textBoxMax.Enabled = false;
- thread = new Thread(noviy_klient);
- thread.Start();
- thread1 = new Thread(Kassa1);
- thread1.Start();
- thread2 = new Thread(Kassa2);
- thread2.Start();
- thread3 = new Thread(Kassa3);
- thread3.Start();
- Start.Enabled = false;
- }
- private void Stop_Click(object sender, EventArgs e)
- {
- thread.Abort();
- thread1.Abort();
- thread2.Abort();
- thread3.Abort();
- Stop.Enabled = false;
- }
- public void pokaz_Ocheredy(object ochered)
- {
- labelQueue.Text = Convert.ToString(ochered);
- }
- public void pokaz_statusa_kassy_1(object ochered1)
- {
- busyness1.Text = Convert.ToString(ochered1);
- }
- public void pokaz_statusa_kassy_2(object ochered2)
- {
- busyness2.Text = Convert.ToString(ochered2);
- }
- public void pokaz_statusa_kassy_3(object ochered3)
- {
- busyness3.Text = Convert.ToString(ochered3);
- }
- public void obsluzheno_vsego_chelovek(object vsego)
- {
- labelCatered.Text = vsego_obsluzheno.ToString();
- }
- public void noviy_klient()
- {
- for (int i = 0; i < max_ochered; i++)
- {
- Random rnd = new Random();
- Thread.Sleep(rnd.Next(3000, 5000));
- ludy_v_ocheredy += 1;
- Invoke(new Ochered(pokaz_Ocheredy), ludy_v_ocheredy);
- }
- }
- public void Kassa1()
- {
- while (true)
- {
- lock (thread)
- {
- if (ludy_v_ocheredy > 0)
- {
- Random rnd_1 = new Random();
- Invoke(new Ochered(pokaz_statusa_kassy_1), "Касса занята");
- ludy_v_ocheredy -= 1;
- Invoke(new Ochered(pokaz_Ocheredy), ludy_v_ocheredy);
- Monitor.Wait(thread, rnd_1.Next(1000, 10000));
- Invoke(new Ochered(obsluzheno_vsego_chelovek), vsego_obsluzheno++);
- Invoke(new Ochered(pokaz_statusa_kassy_1), "Касса свободна");
- }
- }
- }
- }
- public void Kassa2()
- {
- while (true)
- {
- lock (thread)
- {
- if (ludy_v_ocheredy > 0)
- {
- Random rnd_1 = new Random();
- Invoke(new Ochered(pokaz_statusa_kassy_2), "Касса занята");
- ludy_v_ocheredy -= 1;
- Invoke(new Ochered(pokaz_Ocheredy), ludy_v_ocheredy);
- Monitor.Wait(thread, rnd_1.Next(1000, 10000));
- Invoke(new Ochered(obsluzheno_vsego_chelovek), vsego_obsluzheno++);
- Invoke(new Ochered(pokaz_statusa_kassy_2), "Касса свободна");
- }
- }
- }
- }
- public void Kassa3()
- {
- while (true)
- {
- lock (thread)
- {
- if (ludy_v_ocheredy > 0)
- {
- Random rnd_1 = new Random();
- Invoke(new Ochered(pokaz_statusa_kassy_3), "Касса занята");
- ludy_v_ocheredy -= 1;
- Invoke(new Ochered(pokaz_Ocheredy), ludy_v_ocheredy);
- Monitor.Wait(thread, rnd_1.Next(1000, 10000));
- Invoke(new Ochered(obsluzheno_vsego_chelovek), vsego_obsluzheno++);
- Invoke(new Ochered(pokaz_statusa_kassy_3), "Касса свободна");
- }
- }
- }
- }
- }
- }
6.rar
Решение задачи: «Многопоточное приложение: проблема с параллельностью выполнения потоков»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace _6
- {
- public partial class Form1 : Form
- {
- uint max_ochered;
- int ludy_v_ocheredy, vsego_obsluzheno;
- delegate void Ochered(object name);
- Thread thread;
- Thread thread1;
- Thread thread2;
- Thread thread3;
- Mutex mx = new Mutex();
- public Form1()
- {
- InitializeComponent();
- }
- private void Start_Click(object sender, EventArgs e)
- {
- try
- {
- max_ochered = uint.Parse(textBoxMax.Text);
- }
- catch
- {
- if (max_ochered <= 0)
- {
- MessageBox.Show("Ошибка", "Число должно быть положительным");
- }
- textBoxMax.Clear();
- }
- textBoxMax.Enabled = false;
- thread = new Thread(noviy_klient);
- thread.Start();
- thread1 = new Thread(Kassa1);
- thread1.Start();
- thread2 = new Thread(Kassa2);
- thread2.Start();
- thread3 = new Thread(Kassa3);
- thread3.Start();
- Start.Enabled = false;
- }
- private void Stop_Click(object sender, EventArgs e)
- {
- thread.Abort();
- thread1.Abort();
- thread2.Abort();
- thread3.Abort();
- Stop.Enabled = false;
- }
- public void pokaz_Ocheredy(object ochered)
- {
- labelQueue.Text = Convert.ToString(ochered);
- }
- public void pokaz_statusa_kassy_1(object ochered1)
- {
- busyness1.Text = Convert.ToString(ochered1);
- }
- public void pokaz_statusa_kassy_2(object ochered2)
- {
- busyness2.Text = Convert.ToString(ochered2);
- }
- public void pokaz_statusa_kassy_3(object ochered3)
- {
- busyness3.Text = Convert.ToString(ochered3);
- }
- public void obsluzheno_vsego_chelovek(object vsego)
- {
- labelCatered.Text = vsego_obsluzheno.ToString();
- }
- public void noviy_klient()
- {
- for (int i = 0; i < max_ochered; i++)
- {
- Random rnd = new Random();
- Thread.Sleep(rnd.Next(3000, 5000));
- ludy_v_ocheredy += 1;
- Invoke(new Ochered(pokaz_Ocheredy), ludy_v_ocheredy);
- }
- }
- bool GetClient()
- {
- lock (thread)
- {
- if (ludy_v_ocheredy > 0)
- {
- ludy_v_ocheredy -= 1; return true;
- } return false;
- }
- }
- public void Kassa1()
- {
- while (true)
- {
- if (GetClient())
- {
- Random rnd_1 = new Random();
- Invoke(new Ochered(pokaz_statusa_kassy_1), "Касса занята");
- Thread.Sleep(rnd_1.Next(1000, 10000));
- Invoke(new Ochered(obsluzheno_vsego_chelovek), vsego_obsluzheno++);
- Invoke(new Ochered(pokaz_statusa_kassy_1), "Касса свободна");
- }
- }
- }
- public void Kassa2()
- {
- while (true)
- {
- if (GetClient())
- {
- Random rnd_1 = new Random();
- Invoke(new Ochered(pokaz_statusa_kassy_2), "Касса занята");
- Thread.Sleep(rnd_1.Next(1000, 10000));
- Invoke(new Ochered(obsluzheno_vsego_chelovek), vsego_obsluzheno++);
- Invoke(new Ochered(pokaz_statusa_kassy_2), "Касса свободна");
- }
- }
- }
- public void Kassa3()
- {
- while (true)
- {
- if (GetClient())
- {
- Random rnd_1 = new Random();
- Invoke(new Ochered(pokaz_statusa_kassy_3), "Касса занята");
- Thread.Sleep(rnd_1.Next(1000, 10000));
- Invoke(new Ochered(obsluzheno_vsego_chelovek), vsego_obsluzheno++);
- Invoke(new Ochered(pokaz_statusa_kassy_3), "Касса свободна");
- }
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д