zoukankan      html  css  js  c++  java
  • 几种异步操作方式

    1、利用线程池发起异步操作

    View Code
     1 using System;
     2 using System.Threading;
     3 
     4 namespace Asynchronous
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             Console.WriteLine("主线程:准备发起一系列异步操作...");
    11             ThreadPool.QueueUserWorkItem(ComputeBoundOp, 5);
    12             ThreadPool.QueueUserWorkItem(ComputeBoundOp, 7);
    13             Console.WriteLine("主线程:干其它事情...");
    14             Thread.Sleep(1000);
    15             Console.WriteLine("按回车退出...");
    16             Console.ReadLine();
    17         }
    18 
    19         private static void ComputeBoundOp(object o) 
    20         {
    21             Console.WriteLine("异步操作回调:state={0}", o);
    22             Thread.Sleep(1000);
    23         }
    24     }
    25 }
    2、利用Threading.Tasks中的Task
    View Code
     1 using System;
     2 using System.Threading;
     3 using System.Threading.Tasks;
     4 
     5 namespace Asynchronous
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             Console.WriteLine("主线程:准备发起一系列异步操作...");
    12             Task t = new Task(ComputeBoundOp,5);
    13             t.Start();
    14             Console.WriteLine("主线程:干其它事情...");
    15             Thread.Sleep(1000);
    16             Console.WriteLine("按回车退出...");
    17             Console.ReadLine();
    18         }
    19 
    20         private static void ComputeBoundOp(object o) 
    21         {
    22             Console.WriteLine("异步操作回调:state={0}", o);
    23             Thread.Sleep(1000);
    24         }
    25     }
    26 }

    3、利用System.Threading.Timer

    View Code
     1 using System;
     2 using System.Threading;
     3 
     4 
     5 namespace Asynchronous
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             Console.WriteLine("主线程:准备发起一系列异步操作...");
    12             Timer t = new Timer(ComputeBoundOp, 5500);
    13             Console.WriteLine("主线程:干其它事情...");
    14             Thread.Sleep(1000);
    15             Console.WriteLine("按回车退出...");
    16             Console.ReadLine();
    17         }
    18 
    19         private static void ComputeBoundOp(object o) 
    20         {
    21             Console.WriteLine("异步操作回调:state={0}", o);
    22             Thread.Sleep(1000);
    23         }
    24     }
    25 }

    4、利用APM(Asynchronous Programming Model)中的beginXXX方法

    View Code
     1 using System;
     2 using System.Threading;
     3 
     4 namespace Asynchronous
     5 {
     6     class Program
     7     {
     8         delegate void MyDelegate(object o);
     9 
    10         static void Main(string[] args)
    11         {
    12             Console.WriteLine("主线程:准备发起一系列异步操作...");
    13             MyDelegate mydelegate = ComputeBoundOp;
    14             mydelegate.BeginInvoke(null,ComputeBoundOpCallBack,5);             
    15             Console.WriteLine("主线程:干其它事情...");
    16             Thread.Sleep(5000);
    17             Console.WriteLine("按回车退出...");
    18             Console.ReadLine();
    19         }
    20 
    21         private static void ComputeBoundOp(object o) 
    22         {            
    23             Thread.Sleep(1000);//模拟异步操作在做一些耗时的操作
    24         }
    25 
    26         private static void ComputeBoundOpCallBack(IAsyncResult ar) 
    27         {
    28             Console.WriteLine("异步操作的回调:{0}" , ar.AsyncState);
    29             
    30         }
    31     }
    32 }
  • 相关阅读:
    Mysql学习笔记(十四)备份与恢复
    Mysql学习笔记(十三)权限管理
    docker容器持久化卷讲解
    logstash关于date时间处理的几种方式总结
    ELK收集tomcat状态日志
    ELK收集tomcat访问日志并存取mysql数据库案例
    利用fpm制作rpm包
    Elasticsearch一些常用操作和一些基础概念
    Linux Cluster
    LNMP下动静分离部署phpmyadmin软件包
  • 原文地址:https://www.cnblogs.com/mybluesky99/p/2038622.html
Copyright © 2011-2022 走看看