有空帮我看一下,多线程(问题:从不同步的代码块中调用了对象同步方法)

本文通过一个Windows应用程序示例,展示了如何在多线程环境中处理对象同步问题。当按钮被点击时,启动两个线程,一个负责增加数值并更新文本框,另一个负责读取数值并显示。使用Monitor类进行线程同步,确保在特定条件下对共享资源的访问是互斥的,并通过 Pulse 和 Wait 方法控制线程执行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

 

namespace WindowsApplication6_线程同步
{
    public partial class Form1 : Form
    {
        //Forms.Control.CheckForIllegalCrossThreadCalls   =   false;
        public Int64 number;
        Thread thread1;
        Thread thread2;
        public bool fSync = false;
        public common oCommon;
        AddNumberClass addclass;
        ReadNumber readclass;
        public Form1()
        {
            InitializeComponent();
            oCommon = new common();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        Control.CheckForIllegalCrossThreadCalls   =   false;

        }
        private void button1_Click(object sender, EventArgs e)
        {
            addclass = new AddNumberClass(this);
            addclass.oCommon = oCommon;
            thread1 = new Thread(new ThreadStart(addclass.Add));
            thread1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            readclass = new ReadNumber(this);
            readclass.oCommon = oCommon;
            thread2 = new Thread(new ThreadStart(readclass.Read));
            thread2.Start();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            fSync = !fSync;
        }
    }
    public class common
    {

    }

    public class AddNumberClass
    {
        public common oCommon;
        Form1 oform;
        public AddNumberClass(Form1 form)
        {
            oform = form;
        }
        // The nethod that will be called when the thread is started
        public void Add()
        {
            int cLoop = 10;
            //Obtain the lock
            if (oform.fSync)
            {
                Monitor.Enter(oCommon);
            }
            while ((cLoop--) > 0)
            {
                oform.number++;
                Int64 n = oform.number;
                oform.textBox1.Text = n.ToString();

                if (oform.fSync)
                {
                    //Signal for next thread in wait queue to proceed
                    Monitor.Pulse(oCommon);
                }

                Thread.Sleep(100);
                if (oform.fSync)
                {
                    //Realease the lock and wait to be Notified
                    Monitor.Wait(oCommon);
                }
                //Realease the lock
                if (oform.fSync)
                {
                    Monitor.Exit(oCommon);
                }
            }
        }
    }

    public class ReadNumber
    {
        public common oCommon;
        Form1 oform;
        public ReadNumber(Form1 form)
        {
            oform = form;
        }
        public void Read()
        {
            int cLoop = 10;
            if (oform.fSync)
            {
                Monitor.Enter(oCommon);
            }
            while((cLoop--)>0)
            {
                Int64 n=oform.number;
                oform.textBox2.Text=n.ToString();
                if(oform.fSync)
                {
                    //Signal for next thred in wait queue to proceed
                    Monitor.Pulse(oCommon);
                }

                Thread.Sleep(100);
                if(oform.fSync)
                {
                    //Realease the lock and wait to be Notified
                    Monitor.Wait(oCommon);
                }
                if(oform.fSync)
                {
                    //Realease the lock
                    Monitor.Exit(oCommon);
                }

            }
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值