小弟刚学C#。看到有个Thread类。看了MSDN也没怎么看明白,特地来问下:
这个类具体有哪些方法?常用的有哪些?都怎么用?
这个类具体有哪些方法?常用的有哪些?都怎么用?
解决方案 »
- .net中怎样抓取其他网页的内容
- C# 如何调用DataGridViewCellValidatingEventArgs事件,并传入参数
- 在查询数据时,有时要判断数据是否为NULL,下面两种方法,哪个效率高呀?
- DataTextField和DataValueField 搞不清啊
- ScrollBar问题
- C#里如何实现每5分钟按一下"V"键 ?
- C++好难啊
- 怎样在wpf多点触控系统中浏览office,顶者有分!
- 请问:如何使用for循环让2到98偶数相加??
- 请高手看看怎么办?
- DataTable中的数据一次写入到数据库中,也就是只和数据库交互一次,不用循环DataTable一条一条的写入!!!
- winform中datagridview的button修改问题。
1、lock语句
2、Mutex互斥对象
3、
using System;
using System.Threading;
class A
{
public int tickes = 100;
public void Seller()
{//ticket=1
while (true )
{
lock (this)
{
if (tickes == 0)
break;
Console.WriteLine("Seller{0}---{1}", Thread.CurrentThread.ManagedThreadId, tickes);
tickes--;
Thread.Sleep(100);
}
}
}
}
class Test
{
public static void Main()
{
A a = new A();
Thread t1 = new Thread(new ThreadStart(a.Seller));
Thread t2 = new Thread(new ThreadStart(a.Seller));
Thread t3 = new Thread(new ThreadStart(a.Seller));
Thread t4 = new Thread(new ThreadStart(a.Seller));
t1.Start();
t2.Start();
t3.Start();
t4.Start();
Console.Read();
}
}
生产者与消费者同步算法:
同步信号:=false;(没有生产数据)
producter
{
p();
if()//如果为真
生产者等待消费者
生产数据
=true;
通知消费者有数据产生
v();
}
Customer
{
p();
if(!)
消费者等待生产者
消费数据;
=false;
通知生产者
v();
}
using System.Threading;
using System;class ProducterCustomer
{
int x = 0;
int sum = 0;
bool = false ;//没有数据;
public void Producter()
{
for (int i = 1; i <= 100; i++)
{
Monitor.Enter(this);
{
if()
Monitor.Wait(this);
= true;
x = i;
Console.WriteLine("producter--x={0}", x);
Monitor.Pulse(this);//通知消费者改变状态
//Thread.Sleep(5);
}
Monitor.Exit(this);
}
}
public void Customer()
{
for (int i = 1; i <= 100; i++)
{
Monitor.Enter(this);
{
if (!)
Monitor.Wait(this);
= false;
sum += x;
Console.WriteLine("Customer--sum={0}", sum);
Monitor.Pulse(this);
//通知生产者
//Thread.Sleep(5);
}
Monitor.Exit(this);
}
}
}class Test
{
public static void Main()
{
ProducterCustomer pc = new ProducterCustomer();
Thread producter = new Thread(new ThreadStart(pc.Producter));
Thread customer = new Thread(new ThreadStart(pc.Customer));
producter.Start();
customer.Start();
Console.Read();
}
}using System;class AsyncDemo
{
//异步方法
public void Event1()
{
Console.WriteLine("Event1 Start");
System.Threading.Thread.Sleep(4000);
Console.WriteLine("Event1 End");
}
public void Event2()//同步
{
Console.WriteLine("Event2 start");
for (int i = 0; i <= 200; i++)
{
Console.WriteLine(i);
}
Console.WriteLine("Event2 End");
}
}
class Test
{
public delegate void MyDel();
public static void Main()
{
long start = 0;
long end = 0;
start = DateTime.Now.Ticks;
AsyncDemo asy = new AsyncDemo();
asy.Event1();
asy.Event2();
//MyDel del = new MyDel(asy.Event1);
//IAsyncResult ia = del.BeginInvoke(null, null);//步异调用Event1方法
//asy.Event2();//启动同步方法
//del.EndInvoke(ia);//结束异步调用
end = DateTime.Now.Ticks;
Console.WriteLine("时间刻度为{0}",end-start);
Console.Read();
}
}
using System;class AsyncDemo
{
//异步方法
public void Event1()
{
Console.WriteLine("Event1 Start");
System.Threading.Thread.Sleep(4000);
Console.WriteLine("Event1 End");
}
public void Event2()//同步
{
Console.WriteLine("Event2 start");
for (int i = 0; i <= 200; i++)
{
Console.WriteLine(i);
}
Console.WriteLine("Event2 End");
}
}
class Test
{
public delegate void MyDel();
public static void Main()
{
long start = 0;
long end = 0;
start = DateTime.Now.Ticks;
AsyncDemo asy = new AsyncDemo();
asy.Event1();
asy.Event2();
//MyDel del = new MyDel(asy.Event1);
//IAsyncResult ia = del.BeginInvoke(null, null);//步异调用Event1方法
//asy.Event2();//启动同步方法
//del.EndInvoke(ia);//结束异步调用
end = DateTime.Now.Ticks;
Console.WriteLine("时间刻度为{0}",end-start);
Console.Read();
}
}