多线程同时做一件事情的各个部分,一个线程遇到问题,一起停止,如何实现?例子,基于一个多线程例子:
这个应用中,有10个线程同时取1个账户的钱,就当他是卖票吧,一旦票卖完了,如何同时停止10个线程?using System;
using System.Threading;namespace ThreadSimple
{
internal class Account
{
int balance; Random r = new Random(); internal Account(int initial)
{
balance = initial;
}
internal int Withdraw(int amount)
{
if (balance < 0)
{
//如果balance小于0则抛出异常
throw new Exception("Negative Balance");
}
//下面的代码保证在当前线程修改balance的值完成之前
//不会有其他线程也执行这段代码来修改balance的值
//因此,balance的值是不可能小于0 的
lock (this)
{
Console.WriteLine(string.Format("Current Thread:{0},amount={1},balance={2}", Thread.CurrentThread.Name, amount, balance)); //如果没有lock关键字的保护,那么可能在执行完if的条件判断之后
//另外一个线程却执行了balance=balance-amount修改了balance的值
//而这个修改对这个线程是不可见的,所以可能导致这时if的条件已经不成立了
//但是,这个线程却继续执行balance=balance-amount,所以导致balance可能小于0
if (balance >= amount)
{
Thread.Sleep(50);
balance = balance - amount;
return amount;
}
else
{
return 0;
// transaction rejected
}
}
} internal void DoTransactions()
{
for (int i = 0; i < 100; i++)
Withdraw(r.Next(1, 100));
}
} internal class Test
{
static internal Thread[] threads = new Thread[10]; public static void Main()
{
Account acc = new Account(1000); for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions)); threads[i] = t; }
// 线程命名
for (int i = 0; i < 10; i++)
threads[i].Name = i.ToString(); // 线程开始
for (int i = 0; i < 10; i++)
threads[i].Start(); Console.ReadLine(); }
}
}
这个应用中,有10个线程同时取1个账户的钱,就当他是卖票吧,一旦票卖完了,如何同时停止10个线程?using System;
using System.Threading;namespace ThreadSimple
{
internal class Account
{
int balance; Random r = new Random(); internal Account(int initial)
{
balance = initial;
}
internal int Withdraw(int amount)
{
if (balance < 0)
{
//如果balance小于0则抛出异常
throw new Exception("Negative Balance");
}
//下面的代码保证在当前线程修改balance的值完成之前
//不会有其他线程也执行这段代码来修改balance的值
//因此,balance的值是不可能小于0 的
lock (this)
{
Console.WriteLine(string.Format("Current Thread:{0},amount={1},balance={2}", Thread.CurrentThread.Name, amount, balance)); //如果没有lock关键字的保护,那么可能在执行完if的条件判断之后
//另外一个线程却执行了balance=balance-amount修改了balance的值
//而这个修改对这个线程是不可见的,所以可能导致这时if的条件已经不成立了
//但是,这个线程却继续执行balance=balance-amount,所以导致balance可能小于0
if (balance >= amount)
{
Thread.Sleep(50);
balance = balance - amount;
return amount;
}
else
{
return 0;
// transaction rejected
}
}
} internal void DoTransactions()
{
for (int i = 0; i < 100; i++)
Withdraw(r.Next(1, 100));
}
} internal class Test
{
static internal Thread[] threads = new Thread[10]; public static void Main()
{
Account acc = new Account(1000); for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions)); threads[i] = t; }
// 线程命名
for (int i = 0; i < 10; i++)
threads[i].Name = i.ToString(); // 线程开始
for (int i = 0; i < 10; i++)
threads[i].Start(); Console.ReadLine(); }
}
}
{
for (int i = 0; i < 100; i++)
{
int re = Withdraw(r.Next(1, 100));
if(re==0)
break;
}
}
{
for (int i = 0; i < 100; i++)
{
int re = Withdraw(r.Next(1, 100));
Console.WriteLine(string.Format("after Current Thread:{0},balance={1}", Thread.CurrentThread.Name, balance));
if (re == 0 && balance == 0)
{
break;
}
}
}