随拍:红叶

2 Comments

八月让我感动和怀念,如同这片叶子一样。

[singlepic=17725]

C#中CLR线程池(Thread Pool)的使用

No Comments

C#中CLR提供了一个线程池(Thread Pool)的实现,叫做ThreadPool。CLR线程池的主要好处是:

  • 线程池减少了线程创建、开始和停止的次数,提高了效率。
  • 使用线程池可以使得程序员不必考虑多线程编程,而将注意力集中在业务逻辑上。

也有缺点:

  • 线程池中的线程总是后台线程(主线程退出,则所有后台线程也停止,程序结束),而且不能设置优先级(总是ThreadPriority.Normal)。
  • 不能使用一个标识来识别一个特定的线程。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadPoolTest
{
    // 执行工作的类
    class Worker
    {
        // 执行工作的函数
        public void DoWork()
        {
            int threadCode = Thread.CurrentThread.GetHashCode();
            Console.WriteLine(
                       string.Format("[Thread {0}]: Work Starts.", threadCode));
            Thread.Sleep(1000);
            Console.WriteLine(
                       string.Format("[Thread {0}]: Work Ends.", threadCode));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 主线程打印信息
            Console.WriteLine(
                       "Main Thread: " + Thread.CurrentThread.GetHashCode());
            // 构造工作函数
            Worker worker = new Worker();
            // 设置工作入口
            WaitCallback workItem = new WaitCallback(DoCompute);

            // 向CLR线程池提交20个工作
            for (int i = 0; i < 20; i++)
            {
                ThreadPool.QueueUserWorkItem(workItem, worker);
            }

            Console.WriteLine("All Works Queued.");
            Console.ReadLine();
        }

        // 线程池入口委托的实现
        static void DoCompute(object state)
        {
            Worker worker = state as Worker;
            if (worker != null)
            {
                // 执行工作
                worker.DoWork();
            }
        }
    }
}

转瞬即逝

1 Comment

好日子过去了。转瞬即逝。
这就是命运吗?你犯下一个严重的错误,命运就要用一生来惩罚。不是一生连续的痛苦,而是给你一段幸福的时光,然后夺走它。沉浸在幸福当中,试图忘记过去,好像自己从未做错什么一样。而这样的自大最终总是导致,冥冥之中一股力量终于发现了空闲,伸出手,插进你的胸膛,然后挖走你的心。
我想说的是,没有心仍然是可以活的,没有灵魂就不行了。
问题是,没有心,灵魂又能存在多久呢?
说什么呢,呵呵。这些话,都是隐喻,只有这个时刻的我才能够理解,我想将来的我应该也能理解这些,避免我再犯同样的错啊。
我不管到底找寻什么样一个继续活下去的理由,总之找一个。错过一次就永远没有了信用,永远失去了机会,永远是被动的过活。不过,应该也存在流星般的幸福吧。为了一个不知能不能再次重现的机会,我要努力。
呵呵,对于我这样一个追求完美的人来说,我的人生好失败。

C#线程间参数传递的一种实践

No Comments

C#线程间参数传递办法挺多,这里用了一种方式。
传入参数使用ParameterizedThreadStart,传出使用委托,委托也使用ParameterizedThreadStart方式传入线程。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication
{
    // 用于保存要传入Thread的参数
    class ValueParams
    {
        public int IntValue;
        public string StringValue;
        public ReportResultDelegate ReportResult;
        public ValueParams(int intValue, string stringValue,  ReportResultDelegate ReportResult)
        {
            this.IntValue = intValue;
            this.StringValue = stringValue;
            this.ReportResult = ReportResult;
        }
    }

    // 用于向主线程报告结果
    public delegate void ReportResultDelegate(string result);

    class Program
    {
        static void Main(string[] args)
        {
            ValueParams param =  new ValueParams(10, "Test Thread", ReportResult);
            Thread thread = new Thread(new ParameterizedThreadStart(DoPrintValues));
            thread.Start(param);
        }

        // 处理工作线程回报的结果
        private static void ReportResult(string result)
        {
            Console.WriteLine(result);
        }

        // 必须接受object型参数以符合ParameterizedThreadStart委托的要求
        public static void DoPrintValues(object param)
        {
            if (param is ValueParams)
            {
                ValueParams thisParam = (ValueParams)param;
                for (int i = 0; i < thisParam.IntValue; i++)
                {
                    thisParam.ReportResult(thisParam.StringValue);
                }
            }
        }
    }
}

随拍:转瞬即逝的天空

2 Comments

转头向着窗外望去一眼,看到了一副美丽的天空,阴天,但是极富层次感,抓起我的相机,赶紧留下这一瞬间。

[singlepic=17724]

随拍:学校里的树

No Comments

一排只长心形叶子的树,有些虫子,大部分叶子被咬的差不多了,转悠半天终于发现这一棵幼苗,而且是三种颜色的。

[singlepic=17726]

[singlepic=17727]

[singlepic=17728]

随拍:被雨浇了,没白浇吧

1 Comment

赶上上海一阵暴雨,下了一夜。去实验室的路上冒雨拍了些东西,第二天发现这次暴雨,居然还是百年一遇。

[singlepic=17721]

[singlepic=17722]

[singlepic=17723]