• 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);
    }
    }
    }
    }

  • 相关阅读:
    elinput校验利率输入框
    Schema 和 Universal Link
    Oracle数据库恢复删除数据的方法
    element ui 获取table行索引
    愿有人陪你颠沛流离
    古诗词中爱情的模样
    在细雨中呐喊
    麦田里的守望者
    我打碎了夕阳
    工作笔记
  • 原文地址:https://www.cnblogs.com/tsh292278/p/9238212.html
Copyright © 2020-2023  润新知