国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

關(guān)于正則表達(dá)式匹配無異常資源耗盡的解決方案

2019-11-18 16:34:38
字體:
供稿:網(wǎng)友

        在c#中使用正則表達(dá)式進(jìn)行匹配,有時(shí)候我們會(huì)遇到這種情況,cpu使用率100%,但是正則表達(dá)式并沒有異常拋出,正則一直處于匹配過程中,這將導(dǎo)致系統(tǒng)資源被耗盡,應(yīng)用程序被卡住,這是由于正則不完全匹配,而且Regex中沒有Timeout屬性,使正則處理器陷入了死循環(huán)。
        這種情況尤其可能發(fā)生在對(duì)非可靠的被匹配對(duì)象的匹配過程中,例如在我的個(gè)人網(wǎng)站www.eahan.com項(xiàng)目中,對(duì)多個(gè)網(wǎng)站頁面的自動(dòng)采集匹配,就經(jīng)常發(fā)生該問題。為了避免資源耗盡的情況發(fā)生,我寫了一個(gè)AsynchronousRegex類,顧名思義,異步的Regex。給該類一個(gè)設(shè)置一個(gè)Timeout屬性,將Regex匹配的動(dòng)作置于單獨(dú)的線程中,AsynchronousRegex監(jiān)控Regex匹配超過Timeout限定時(shí)銷毀線程。


using System;

using System.Text.RegularExPRessions;
using System.Threading;

namespace LZT.Eahan.Common
{
    public class AsynchronousRegex
    {
        private MatchCollection mc;
        private int _timeout;        // 最長(zhǎng)休眠時(shí)間(超時(shí)),毫秒
        private int sleepCounter;
        private int sleepInterval;    // 休眠間隔,毫秒
        private bool _isTimeout;

        public bool IsTimeout
        {
            get {return this._isTimeout;}
        }

        public AsynchronousRegex(int timeout)
        {
            this._timeout = timeout;
            this.sleepCounter = 0;
            this.sleepInterval = 100;
            this._isTimeout = false;

            this.mc = null;
        }

        public MatchCollection Matchs(Regex regex, string input)
        {
            Reg r = new Reg(regex, input);
            r.OnMatchComplete += new Reg.MatchCompleteHandler(this.MatchCompleteHandler);
           
            Thread t = new Thread(new ThreadStart(r.Matchs));
            t.Start();

            this.Sleep(t);

            t = null;
            return mc;
        }

        private void Sleep(Thread t)
        {
            if (t != null && t.IsAlive)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(this.sleepInterval));
                this.sleepCounter ++;
                if (this.sleepCounter * this.sleepInterval >= this._timeout)
                {
                    t.Abort();
                    this._isTimeout = true;
                }
                else
                {
                    this.Sleep(t);
                }
            }
        }

        private void MatchCompleteHandler(MatchCollection mc)
        {
            this.mc = mc;
        }

        class Reg
        {
            internal delegate void MatchCompleteHandler(MatchCollection mc);
            internal event MatchCompleteHandler OnMatchComplete;

            public Reg(Regex regex, string input)
            {
                this._regex = regex;
                this._input = input;
            }

            private string _input;
            public string Input
            {
                get {return this._input;}
                set {this._input = value;}
            }

            private Regex _regex;
            public Regex Regex
            {
                get {return this._regex;}
                set {this._regex = value;}
            }

            internal void Matchs()
            {
                MatchCollection mc  = this._regex.Matches(this._input);
                if (mc != null && mc.Count > 0)    // 這里有可能造成cpu資源耗盡
                {
                    this.OnMatchComplete(mc);
                }
            }
        }
    }
}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹凤县| 九龙县| 镇宁| 安岳县| 大悟县| 林周县| 奈曼旗| 兴海县| 鹿泉市| 甘泉县| 晋中市| 桃园市| 水富县| 惠东县| 辽宁省| 武宣县| 濮阳市| 平罗县| SHOW| 开阳县| 文登市| 西贡区| 三门峡市| 余干县| 子长县| 突泉县| 长垣县| 普兰店市| 会同县| 陆河县| 郯城县| 常熟市| 锡林郭勒盟| 双桥区| 长岛县| 长武县| 东乌珠穆沁旗| 西乡县| 盐边县| 西乡县| 乐至县|