惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Secure Thoughts
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
D
Docker
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
TaoSecurity Blog
TaoSecurity Blog
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
Cyberwarzone
Cyberwarzone
V
V2EX
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
WordPress大学
WordPress大学
P
Palo Alto Networks Blog
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
N
News and Events Feed by Topic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
Engineering at Meta
Engineering at Meta
S
Schneier on Security
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Hacker News: Ask HN
Hacker News: Ask HN
小众软件
小众软件
博客园 - 聂微东
S
Security Affairs
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
T
Threatpost
博客园 - 【当耐特】
L
LINUX DO - 热门话题
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
A
About on SuperTechFans
F
Fortinet All Blogs

博客园 - s80895304

win8 framework3.5安装 ChangeCharToTable sql 触发器 mysql 备忘sql ActiveMQ 安装 【转】j2ee 环境搭建 C#原始类型扩展方法—this参数修饰符 消息队列(Message Queue)简介及其使用 IE中同一个url第二次AJAX调用无法触发onreadystatechange事件 Web Service 身份验证 sql 索引 Memcached、MongoDB、Redis和tokyotyrant 【转】ActiveMQ redis iphone 技巧 10款iOS高效开发必备的Objective-C类库 ASIHTTPRequest jquery 倒计时插件 js keycode
Queue 队列 写日志简单应用
s80895304 · 2011-10-14 · via 博客园 - s80895304

public class LogContent
    {
        public LogContent()
        {
           
        }
        public LogContent(string msg)
        {
            this.Message = msg;
        }
        private string mssage;
        public string Message
        {
            get;
            set;
        }
    }

public static class FileQueueManager
    {
        private static object lockHelper = new object();
        static Thread workerThread;
        static FileQueueManager()
        {
            workerThread = new Thread(new ThreadStart(Run));
            workerThread.Start();
        }
        private static Queue<LogContent> logMessageQueue = new Queue<LogContent>();

        public static void Write(LogContent log)
        {
            lock (lockHelper)
            {
                logMessageQueue.Enqueue(log);
            }
        }
        public static int QueueCount
        {
            get { return logMessageQueue.Count; }
        }
        private static void Run()
        {
            lock (lockHelper)
            {
                Write(new LogContent() { Message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") });
            }
            LogContent log;
            while (true)
            {
                try
                {
                    if (logMessageQueue.Count > 0)
                    {
                        Write();
                        lock (lockHelper)
                        {
                            logMessageQueue.Dequeue();
                        }
                    }
                    else
                        break;
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {

                }
            }
        }
        private static void Write()
        {
            string logdir;
            //logdir = System.Web.HttpContext.Current.Server.MapPath(logFileName);
            logdir = @"d:\a.log";
            try
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(logdir, true);
                sw.BaseStream.Seek(0, System.IO.SeekOrigin.End);
                sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + DateTime.Now.Millisecond + "]" + workerThread.ManagedThreadId + "");
                sw.Flush();
                sw.Close();
            }
            catch (Exception e)
            {

            }
        }

    }

private static void ThreadQueueTest()
        {
            for (int i = 0; i < 100; i++)
                FileQueueManager.Write(new LogContent() { Message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") });
            System.Console.WriteLine(FileQueueManager.QueueCount);
        }