先看看CS类后面源码!public delegate void Treadmoden(int n);
public class Account
{
public static int balance;
private Treadmoden DLtread;
Random random = new Random();
public Account(int initial,Treadmoden tt)
{
balance = initial;
DLtread = tt;
// HttpContext.Current.Response.Write("存款不足!");
} void withdraw(int amount)
{
if (DLtread != null) DLtread(amount);
}
public void transactions()
{for (int i=0;i<4;i++)
withdraw(random.Next(1,500));
}ASPX后台源码 public void readmoney(int money)
{
if (Account.balance < 0)
Response.Write("存款不足");
lock (this)
{
// Thread.Sleep(100);//为什么这句话加上就不正确,出现Response.Write响应在此上下文中不可用!
//在控制台的CS中就可以加上这句,不知为什么
Response.Write(Thread.CurrentThread.Name.ToString() + " ");
if (Account.balance >= money)
{
Response.Write("提前" + Account.balance.ToString());
Response.Write("提了" + money.ToString());
Account.balance = Account.balance - money;
Response.Write("提后" + Account.balance.ToString()+"<br/>");
}
else
{ Response.Write("存款不足!" + Account.balance.ToString() + "<br/>"); }
}
}
protected void Page_Load(object sender, EventArgs e)
{
Thread[] threads = new Thread[2];
Account account = new Account(1000, new Treadmoden(readmoney));
for (int i = 0; i < 2; i++)
{
threads[i] = new Thread(new ThreadStart(account.transactions));
threads[i].Name = "thread"+i;
}
for (int i = 0; i < 2; i++)
threads[i].Start();
}
public class Account
{
public static int balance;
private Treadmoden DLtread;
Random random = new Random();
public Account(int initial,Treadmoden tt)
{
balance = initial;
DLtread = tt;
// HttpContext.Current.Response.Write("存款不足!");
} void withdraw(int amount)
{
if (DLtread != null) DLtread(amount);
}
public void transactions()
{for (int i=0;i<4;i++)
withdraw(random.Next(1,500));
}ASPX后台源码 public void readmoney(int money)
{
if (Account.balance < 0)
Response.Write("存款不足");
lock (this)
{
// Thread.Sleep(100);//为什么这句话加上就不正确,出现Response.Write响应在此上下文中不可用!
//在控制台的CS中就可以加上这句,不知为什么
Response.Write(Thread.CurrentThread.Name.ToString() + " ");
if (Account.balance >= money)
{
Response.Write("提前" + Account.balance.ToString());
Response.Write("提了" + money.ToString());
Account.balance = Account.balance - money;
Response.Write("提后" + Account.balance.ToString()+"<br/>");
}
else
{ Response.Write("存款不足!" + Account.balance.ToString() + "<br/>"); }
}
}
protected void Page_Load(object sender, EventArgs e)
{
Thread[] threads = new Thread[2];
Account account = new Account(1000, new Treadmoden(readmoney));
for (int i = 0; i < 2; i++)
{
threads[i] = new Thread(new ThreadStart(account.transactions));
threads[i].Name = "thread"+i;
}
for (int i = 0; i < 2; i++)
threads[i].Start();
}
解决方案 »
- OA系统
- IE6下的table布局问题
- ashx 处理图片
- 求文本框内必须是数字而且不能为空的正则表达式
- 弱弱地问,用MS的asp.net 2.0 ajax extentions做的网站,在服务器端要安装MS的asp.net 2.0 ajax extentions吗?
- 在我的论坛中如何控件别人发贴时的链接图片的大小
- 我初学写文件下载代码,出老个问题想请教高手
- 大侠们帮偶看下这个<asp:HyperLink>控件有什么错误
- 请问怎么Reset整个页面上的控件到初始状态,而验证控件不报错信息。
- 小女子再请教一个问题
- 如何查看XP系统电脑里是否安装了.net framework??
- 请教一个导出数据到Excel模板的问题~~·急
运行结果如下
thread0 提前1000提了154提后846
thread0 提前846提了310提后536
thread0 提前536提了116提后420
thread0 提前420提了267提后153
thread1 提前153提了70提后83
thread1 存款不足!83
thread1 存款不足!83
thread1 存款不足!83
但多了一个Thread.Sleep(100);就不对了
有人提出线程异步,不知怎么用法!也不知可行否!
在Page_Load执行完成后,用户已经看到页面了,此时你还能向他浏览器写东西?