Thread(C#)
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace RailWayStation
{
class Program
{
static void Main(string[] args)
{
Station NewYorkCity = new Station();
Thread t1=new Thread(new ThreadStart(NewYorkCity.SellTicket));//新例項化了個執行緒的物件然後再new了個執行緒開始,再把方法加進去
Thread t2=new Thread(new ThreadStart(NewYorkCity.SellTicket));
Thread t3 = new Thread(new ThreadStart(NewYorkCity.SellTicket));
t1.Name = "1號視窗";
t2.Name = "2號視窗";
t3.Name = "3號視窗";
t1.Start();
t2.Start();
t3.Start();
}
}
class Station
{
int ticket = 100;//火車站有?張票
public void SellTicket()
{
while (true)
{
lock (this)
{
if (ticket > 0)
{
Console.WriteLine(Thread.CurrentThread.Name + "第" + ticket.ToString() + "張票");
ticket--;
}
else { break; }
}
}
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10395457/viewspace-980397/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Thread2(C#)threadC#
- c# 執行緒Thread的IsBackground屬性C#執行緒thread
- C#中Thread.Sleep()的作用及用法C#thread
- 關於C#中Thread.Join()的一點理解C#thread
- C# Thread.Sleep 不精準的問題以及解決方案C#thread
- C#多執行緒詳解(一) Thread.Join()的詳解C#執行緒thread
- Threadthread
- Hello,Threadthread
- Thread類thread
- Thread jointhread
- [深入學習C#]C#實現多執行緒的方法:執行緒(Thread類)和執行緒池(ThreadPool)C#執行緒thread
- Thread.jointhread
- Thread知識thread
- redolog threadthread
- tomcat one connection one thread one request one threadTomcatthread
- C#關於在返回值為Task方法中使用Thread.Sleep引發的思考C#thread
- thread local in pythonthreadPython
- Hystrix Thread Pool 解析thread
- Thread類及使用thread
- Thread(執行緒)thread執行緒
- DUBBO Thread pool is EXHAUSTED!thread
- Thread原始碼剖析thread原始碼
- 深入Thread.sleepthread
- mysql 的thread poolMySqlthread
- (不要)使用std::threadthread
- Java Thread in JVM (轉)JavathreadJVM
- Thread already joined at **thread
- jmeter報錯“Uncaught exception in thread Thread[AWT-EventQueue-0,6,main]“JMeterExceptionthreadAI
- alter database disable thread 2Databasethread
- Thread的interrupt機制thread
- Java-關於ThreadJavathread
- Thread.Abort()的陷阱thread
- 如何分析java Thread DUMPJavathread
- kill_mysql_sleep_threadMySqlthread
- No Hibernate Session bound to threadSessionthread
- Initianization Parameter (5) : THREADthread
- rt-thread bootloadthreadboot
- (RT-Thread學習筆記1)基於 CubeMX 移植 RT-Thread Nanothread筆記NaN