zoukankan      html  css  js  c++  java
  • 线程池管理线程及线程间通信

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

    namespace ConsoleTest
    {
    public class ClassThread
    {
    //监听线程事件
    private ManualResetEvent myResetEvent = new ManualResetEvent(false);
    private ManualResetEvent myResetEvent2 = new ManualResetEvent(false);
    public bool set()
    {
    myResetEvent.Set();
    return true;
    }

    public bool set2()
    {
    myResetEvent2.Set();
    return true;
    }

    private void ThreadOne(object stateInfo)
    {

    for (int i = 0; i < 100; i++)
    {
    Console.WriteLine("123");
    }
    set();
    myResetEvent2.WaitOne();
    Console.WriteLine("123!!!!!!!!!");
    }
    private void ThreadTwo(object stateInfo)
    {

    myResetEvent.WaitOne();
    Console.WriteLine("456");

    Func<bool> func = new Func<bool>(MethodTwo);
    func.Invoke();

    Console.WriteLine("901");
    Console.WriteLine("901");
    Console.WriteLine("901");

    }
    private bool MethodTwo()
    {
    Console.WriteLine("0000000000000000000000000000000000000000000000");

    return set2();
    }
    public void run()
    {
    Thread _ThreadAsyncRunner = new Thread(new ThreadStart(ThreadProc_AsyncRunner));
    _ThreadAsyncRunner.Start();
    }
    private void ThreadProc_AsyncRunner()
    {
    ThreadPool.QueueUserWorkItem(new WaitCallback(this.ThreadOne));
    ThreadPool.QueueUserWorkItem(new WaitCallback(this.ThreadTwo));
    }
    }
    }

    test:

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

    namespace ConsoleTest
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.SetBufferSize(100,1200);
    ClassThread threads = new ClassThread();
    threads.run();
    Console.WriteLine("11111");
    Console.ReadKey();
    }
    }
    }

  • 相关阅读:
    [BZOJ 1095] [ZJOI 2007]Hide 捉迷藏
    [BZOJ 2759] 一个动态树好题
    BZOJ 3122 SDOI2013 随机数生成器
    [NOIP集训]10月18日
    [NOIP集训]10月17日
    [NOIP集训]10月16日
    [NOI题库]1.3编程基础之算术表达式与顺序执行 题解(一)
    [NOI题库]1.2编程基础之变量定义、赋值及转换 题解
    [NOI题库]1.1编程基础之输入输出 题解
    [作业]排序算法练习(二)
  • 原文地址:https://www.cnblogs.com/lxc-binary/p/3979459.html
Copyright © 2011-2022 走看看