zoukankan      html  css  js  c++  java
  • c#(asp.net) 多线程示例,用于同时处理多个任务

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Threading;
    using System.Web.UI.WebControls;
    
    public partial class muti_thread : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Thread Thread1 = new Thread(new ThreadStart(CalcSum));
            Thread Thread2 = new Thread(new ThreadStart(CalcGap));
    
            Thread1.Start();
            Thread2.Start();
    
            Thread1.Join();
            Thread2.Join();
        }
    
    
        //求和方法  
        protected  void CalcSum()
        {
            long sum = 0;
            for (long i = 0; i < 100; i++)
            {
                sum += i;
                Response.Write(string.Format("Thread1-->i={0}:sum={1}<br/>", i, sum));
                Response.Flush();
                System.Threading.Thread.Sleep(5000);            
            }
        }
    
        //求差方法  
        protected void CalcGap()
        {
            long gap = 0;
            for (long i = 100; i >= 0; i--)
            {
                gap = i - 1;
                Response.Write(string.Format("Thread2-->i={0}:gap={1}<br/>", i, gap));
                Response.Flush();
                System.Threading.Thread.Sleep(1000);
            }
        }
    }



  • 相关阅读:
    宋宝华: 文件读写(BIO)波澜壮阔的一生【转】
    内核工具 – Sparse 简介【转】
    【java】JSON.toJSONString 空对象也可以转化为JSON字符串
    Seata分布式事务简单使用
    Mixin 工作原理
    公链
    公链
    公链
    公链
    公链
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234133.html
Copyright © 2011-2022 走看看