zoukankan      html  css  js  c++  java
  • C#多线程编程实战1.7前台线程和后台线程

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    //前台线程和后台线程
    namespace Recipe7
    {
    class Program
    {
    static void Main(string[] args)
    {
    var sampleForeground=new ThreadSample(10);
    var sampleBackground=new ThreadSample(20);

    var threadOne = new Thread(sampleForeground.CountNumbers);
    threadOne.Name = "ForegroundThread";
    var threadTwo = new Thread(sampleBackground.CountNumbers);
    threadTwo.Name = "BackgroundThread";
    threadTwo.IsBackground = true;

    threadOne.Start();
    threadTwo.Start();

    Console.ReadKey();
    }
    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    namespace Recipe7
    {
    public class ThreadSample
    {
    private readonly int _iterations;
    public ThreadSample(int iterations)
    {
    _iterations = iterations;
    }
    public void CountNumbers()
    {
    for (int i = 0; i < _iterations; i++)
    {
    Thread.Sleep(TimeSpan.FromMilliseconds(500));
    Console.WriteLine("currentThread.Name:{0} prints{1}",Thread.CurrentThread.Name,i);
    }
    }
    }
    }

  • 相关阅读:
    进程与线程
    the art of seo(chapter seven)
    the art of seo(chapter six)
    the art of seo(chapter five)
    the art of seo(chapter four)
    the art of seo(chapter three)
    the art of seo(chapter two)
    the art of seo(chapter one)
    Sentinel Cluster流程分析
    Sentinel Core流程分析
  • 原文地址:https://www.cnblogs.com/tsh292278/p/9238212.html
Copyright © 2011-2022 走看看