国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

多線程的使用

2019-11-08 18:28:16
字體:
來源:轉載
供稿:網友
using System;using System.Threading;namespace _15._1thread{ class PRogram { public static void Test() { Console.WriteLine("{0}正在執行測試方法",Thread.CurrentThread.Name); } static void Main(string[] args) { ThreadStart threadWork = new ThreadStart(Test); Thread thread1 = new Thread(threadWork); thread1.Name = "線程1"; Thread thread2 = new Thread(threadWork); thread2.Name = "線程2"; Thread thread3 = new Thread(threadWork); thread3.Name = "線程3"; //開始執行 thread3.Start(); thread2.Start(); thread1.Start(); Console.ReadKey(); } }}

一個可能的情況,因為多線程是并行的,不能確定每一次具體的運行情況 這里寫圖片描述 volatile易失域,多線程-賣票系統

using System;using System.Threading;namespace _15._2volatile{ class Program { //定義易失域 車票數量 public volatile static int tickets = 10; //賣票 public static void SellTickets() { //票數大于10才能繼續賣票 while (tickets>0) { tickets--; Console.WriteLine("{0},賣了一張票,剩余票數{1}",Thread.CurrentThread.Name,tickets); } } static void Main(string[] args) { Thread thread1 = new Thread(SellTickets); thread1.Name = "窗口1"; Thread thread2 = new Thread(SellTickets); thread2.Name = "窗口2"; Thread thread3 = new Thread(SellTickets); thread3.Name = "窗口3"; //開始執行 thread1.Start(); thread2.Start(); thread3.Start(); Console.ReadKey(); } }}

使用3個線程模擬3個售票窗口,雖然線程是并發的,但是他們對于易失域的訪問時需要排隊的,所以不會出現多賣票或者少賣票的情況, 可以定義為易失域的數據類型為:引用類型,數據值類型,枚舉類型和bool類型 這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 许昌县| 抚远县| 红安县| 叙永县| 辽源市| 海门市| 宿迁市| 会理县| 天水市| 柳林县| 吴忠市| 吉木萨尔县| 武威市| 化州市| 三门峡市| 四会市| 鸡泽县| 定州市| 西安市| 望城县| 康保县| 资中县| 嘉善县| 盐源县| 定西市| 中西区| 阿克苏市| 榆树市| 临城县| 丹凤县| 武乡县| 新竹县| 合山市| 洛阳市| 苏尼特右旗| 太仓市| 西峡县| 定陶县| 乌鲁木齐县| 松溪县| 隆尧县|