none
多线程,关于同步和override RRS feed

  • 问题

  • //能"同步"和"异步"两种版本的类.

    class Demo { private ShareState _state; public Demo() { } public Demo(ShareState state) { this._state = state; } public virtual void DoTheJob() { //异步方法. for(int i = 0; i < 50000; i++) { lock(_state) //记住在这里也要同步(一个任务不一定是一个线程). _state.State += 1; } } public static Demo Synchronized(Demo demo) { //同步对象. if(!demo.IsSynchronized) return new SynchronizationDemo(demo); return demo; } public virtual bool IsSynchronized { //异步属性. get { return false; } } private class SynchronizationDemo : Demo { private object syncRoot = new object(); //用于同步的对象. private Demo d; public override bool IsSynchronized { //同步属性. get { return true; } } public override void DoTheJob() { //同步方法. lock(syncRoot) { //d.DoTheJob(); base.DoTheJob(); } } public SynchronizationDemo(Demo d) { this.d = d; } } } //Call in the Main(): ShareState state = new ShareState(); //用于同步(锁定)的对象. int numTask = 20; Task[] tasks = new Task[numTask]; //任务数组. for(int i = 0; i < tasks.Length; i++) { Demo d = new Demo(state); d = Demo.Synchronized(d); //同步对象. tasks[i] = new Task(d.DoTheJob); tasks[i].Start(); } for(int i = 0; i < tasks.Length; i++) tasks[i].Wait(); Console.Write(state.State);

    主函数中, tasks[i].Wait();会抛异常,如果在 Demo类中的重写方法:public override void DoTheJob() {   //同步方法.
                    lock(syncRoot) {
                        //d.DoTheJob();
                        base.DoTheJob();
                    }
                }
    如果是"base.DoTheJob();".
    但是如果是"//d.DoTheJob();"的话,就正常...想不通...
    
    
    2,还有一个问题,
    这里设计的 SynchronizationDemo为 private有什么意义哈?
    对外,貌似那些重写的方法又不可见...
    是不是类是private,但是那些方法是public(只是为了让父类Demo"使用?




    2013年6月20日 10:58

答案

  • @如果是"base.DoTheJob();".

    因为你调用了base方法,而base的state变量没有初始化(为null):

     public virtual void DoTheJob()
            { 
                for (int i = 0i < 50000i++)
                {
                    lock (_state)    //此处为空!!!
                        _state.State += 1;
                }
            }



    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月20日 12:47
    版主

全部回复

  • 解决方法:http://social.msdn.microsoft.com/Forums/zh-CN/d443aa09-4040-436c-a80b-be4398035b27

    @这里设计的 SynchronizationDemo private有什么意义哈?

    很明显,作者不想让客户直接访问这个类,所以通过在Demo定义一个同步方法返回这个同步的Demo子类。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月20日 12:41
    版主
  • @如果是"base.DoTheJob();".

    因为你调用了base方法,而base的state变量没有初始化(为null):

     public virtual void DoTheJob()
            { 
                for (int i = 0i < 50000i++)
                {
                    lock (_state)    //此处为空!!!
                        _state.State += 1;
                }
            }



    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月20日 12:47
    版主
  • 嗯嗯....我这样想...但是不懂对不对...所以问下...
    2013年6月20日 15:37
  • 哦..这个没有想到...谢谢...
    2013年6月20日 15:38