有空幫我看一下,多線程(問題:從不同步的代碼塊中調用了對象同步方法)

來源:互聯網
上載者:User

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);
                }

            }
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.