zoukankan      html  css  js  c++  java
  • C# 多线程操作队列

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Net.Sockets;
    using System.IO;
    using System.Collections;

    namespace ConsoleApplication1
    {

    public class prodert
    {
    private string urls;
    private string name;
    public string Name
    {
    set { this.name = value; }
    get { return this.name; }
    }

    public string Urls
    {
    set { this.urls = value; }
    get { return this.urls; }
    }
    }

    class IPSearch
    {
    static Queue<prodert> q = new Queue<prodert>();
    public static object a = new object();
    static void Main(string[] args)
    {

    Dictionary<string, string> dic = new Dictionary<string, string>();

    for (int i = 0; i < 100; i++)
    {
    prodert p = new prodert();
    p.Urls = "http://www.hao123.com" + i.ToString();
    p.Name = "hao123com" + i.ToString();
    q.Enqueue(p);
    }

    Thread t1 = new Thread(new ThreadStart(getQueue));
    Thread t2 = new Thread(new ThreadStart(getQueue));
    Thread t3 = new Thread(new ThreadStart(getQueue));
    t1.Name = "t1";
    t1.Start();
    t2.Name = "t2";
    t2.Start();
    t3.Name = "t3";
    t3.Start();
    Console.ReadKey();
    }
    private static void getQueue()
    {
    for (int i = 0; i < 10; i++)
    {
    aa();
    }
    }
    private static void aa()
    {
    lock (a)
    {
    if (q.Count > 0)
    {
    prodert p1 = q.Dequeue();
    Thread.Sleep(100);
    Console.WriteLine(p1.Name + p1.Urls+"------"+Thread.CurrentThread.Name);

    }
    }
    }
    }
    }

  • 相关阅读:
    【noip2012】开车旅行
    【AC自动机】专题总结
    【noi2013】【bz3244】树的计数
    BZOJ1069: [SCOI2007]最大土地面积
    BZOJ1185: [HNOI2007]最小矩形覆盖
    BZOJ1047: [HAOI2007]理想的正方形
    BZOJ1801: [Ahoi2009]chess 中国象棋
    BZOJ1925: [Sdoi2010]地精部落
    BZOJ1057: [ZJOI2007]棋盘制作
    BZOJ1217: [HNOI2003]消防局的设立
  • 原文地址:https://www.cnblogs.com/armyfai/p/3761553.html
Copyright © 2011-2022 走看看