国产探花免费观看_亚洲丰满少妇自慰呻吟_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類型 這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荔波县| 灵璧县| 南川市| 临城县| 玉屏| 玛纳斯县| 新乡市| 卢龙县| 威远县| 霍林郭勒市| 阿克苏市| 平陆县| 繁峙县| 仙桃市| 大方县| 旌德县| 海林市| 原平市| 远安县| 昌图县| 凯里市| 吉木萨尔县| 辉南县| 登封市| 白沙| 溧水县| 全南县| 咸阳市| 新民市| 广德县| 周宁县| 洛宁县| 昌都县| 仲巴县| 望城县| 长岭县| 海安县| 济阳县| 东阳市| 莱州市| 福清市|