码迷,mamicode.com
首页 > 编程语言 > 详细

造轮子:线程的小用

时间:2016-01-08 23:20:43      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

厂址:http://www.cnblogs.com/sandy_liao/archive/2010/11/29/1891533.html

在开发中经常会遇到线程的例子,如果某个后台操作比较费时间,我们就可以启动一个线程去执行那个费时的操作,同时程序继续执行。在某些情况下可能会出现多个线程的同步协同的问题,下面的例子就展示了在两个线程之间如何协同工作。

这个程序的思路是共同做一件事情(从一个ArrayList中删除元素),如果执行完成了,两个线程都停止执行。

代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

/// <summary>
/// 在开发中经常会遇到线程的例子,如果某个后台操作比较费时间,我们就可以启动一个线程去执行那个费时的操作,同时程序继续执行。在某些情况下可能会出现多个线程的同步协同的问题,下面的例子就展示了在两个线程之间如何协同工作。
///
///这个程序的思路是共同做一件事情(从一个ArrayList中删除元素),如果执行完成了,两个线程都停止执行。
///作者:周公
/// 时间:2008-5-17
/// 原发地址:http://blog.csdn.net/zhoufoxcn
/// </summary>
public class ThreadDemo
{
    private Thread[] threads;
    private int thrs = 5;
    private ArrayList stringList;
    private event EventHandler OnNumberClear;//数据删除完成引发的事件
    public static void Main()
    {
        ThreadDemo demo = new ThreadDemo(100);
        demo.Action();
    }
    public ThreadDemo(int number)
    {
        Random random = new Random(1000000);
        stringList = new ArrayList(number);
        for (int i = 0; i < number; i++)
        {
            stringList.Add(i.ToString());
        }
        threads = new Thread[thrs];
        for (int i = 0; i < thrs; i++)
        {
            threads[i] = new Thread(new ThreadStart(Run));
            threads[i].Name = "线程" + (i + 1);
        }
        OnNumberClear += new EventHandler(ThreadDemo_OnNumberClear);

    }
    /// <summary>
    /// 开始工作
    /// </summary>
    public void Action()
    {
        for (int i = 0; i < thrs; i++)
        {
            threads[i].Start();
        }
    }
    /// <summary>
    /// 共同做的工作
    /// </summary>
    private void Run()
    {
        string stringValue = null;
        while (true)
        {
            Monitor.Enter(this);//锁定,保持同步
            stringValue = (string)stringList[0];
            Console.WriteLine(Thread.CurrentThread.Name + "删除了" + stringValue);
            stringList.RemoveAt(0);//删除ArrayList中的元素
            if (stringList.Count == 0)
            {
                OnNumberClear(this, new EventArgs());//引发完成事件
            }
            Monitor.Exit(this);//取消锁定
            Thread.Sleep(5);
        }
    }

    //执行完成之后,停止所有线程
    void ThreadDemo_OnNumberClear(object sender, EventArgs e)
    {
        Console.WriteLine("执行完了,停止了所有线程的执行。");
        for (int i = 0; i < thrs; i++)
        {
            threads[i].Abort();
        }
    }
}
 

用例:

using PMS.DBEntity;
using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace PMS.Convert.PDF
{
    public struct FileInfo
    {
        public int fileid;
        public string filepath;
    }


    public class ConvertSwf
    {
        static Queue<FileInfo> filelist = new Queue<FileInfo>();
        private static string rootpath = HttpContext.Current.Server.MapPath("~/");
        private static Thread t = null;
        private static string conn = "";
        public static void LoadQueue(int classid, string _conn)
        {
            conn = _conn;
            using (var entity = new PMSEntities(conn))
            {
                DALContent dal = new DALContent();
                var fileinfo = (from p in entity.jcms_normal_content
                                where p.ClassId == classid && p.IsPass == 1 && p.CustomField05.Substring(0, 10) == "pdf" && p.CustomField03 != null
                                select p);

                if (!fileinfo.Any())
                {
                    return;
                }
                foreach (var item in fileinfo)
                {
                    filelist.Enqueue(new FileInfo { fileid = item.Id, filepath = rootpath + "" + item.CustomField03.Remove(0, 1) });
                    item.CustomField05 = "convet";
                }
                entity.SaveChanges();
            }
        }
        public static void ExecTreade()
        {
            try
            {
                if (t == null)
                {
                    t = new Thread(Run);
                    t.Start();
                }
                if (!t.IsAlive)
                {
                    t = new Thread(Run);
                    t.Start();
                }
            }
            catch (Exception)
            {

                throw;
            }

        }

        public static string sourcefile = "";
        static string outfile = "";
        static void Run()
        {
            Pdf2Swf pdf2swf = new Pdf2Swf();
            string saveName = "";
            string extension = "";
            string outfile = "";
            int id = 0;
            using (var entity = new PMSEntities(conn))
            {
                while (filelist.Count > 0)
                {
                    FileInfo f = filelist.Dequeue(); ;
                    sourcefile = f.filepath;
                    id = f.fileid;
                    saveName = Path.GetFileName(sourcefile);
                    extension = Path.GetExtension(sourcefile).ToLower();
                    saveName = saveName.Substring(0, saveName.Length - extension.Length);
                    outfile = Path.GetDirectoryName(sourcefile) + "/" + saveName + "_%.swf";
                    try
                    {
                        pdf2swf.PDFConvertToSWF(sourcefile, outfile);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                        continue;
                    }
                    var content = entity.jcms_normal_content.SingleOrDefault(p => p.Id == id);
                    if (File.Exists(outfile.Replace("_%", "_1")))
                    {
                        content.CustomField04 = "/" + outfile.Replace(rootpath, "").Replace("_%", "_[*,0]");
                        content.CustomField05 = "complete";
                        entity.SaveChanges();
                    }
                    else
                    {
                        content.CustomField04 = "";
                        content.CustomField05 = "error";
                        entity.SaveChanges();
                    }
                }
            }
             
            t.Abort();

        }
    }
}

 

造轮子:线程的小用

标签:

原文地址:http://www.cnblogs.com/shy1766IT/p/5115008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!