谁给我一个简单的多线程例子,要有源码,马上给分

christ2 2004-06-17 03:48:12
如题
...全文
322 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SwordGrass 2004-06-24
  • 打赏
  • 举报
回复
关注一下吧,虽然还是不会
hnxyy 2004-06-18
  • 打赏
  • 举报
回复
mark
------------------------------------
试用版广告信息(正式版将取消)
CSDN新版论坛助手:http://community.csdn.net/Expert/TopicView.asp?id=3103834
boytomato 2004-06-17
  • 打赏
  • 举报
回复
using System;
using System.Threading;

public class Alpha
{

// This method that will be called when the thread is started
public void Beta()
{
while (true)
{
Console.WriteLine("Alpha.Beta is running in its own thread.");
}
}
};

public class Simple
{
public static int Main()
{
Console.WriteLine("Thread Start/Stop/Join Sample");

Alpha oAlpha = new Alpha();

// Create the thread object, passing in the Alpha.Beta method
// via a ThreadStart delegate. This does not start the thread.
Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));

// Start the thread
oThread.Start();

// Spin for a while waiting for the started thread to become
// alive:
while (!oThread.IsAlive);

// Put the Main thread to sleep for 1 millisecond to allow oThread
// to do some work:
Thread.Sleep(1);

// Request that oThread be stopped
oThread.Abort();

// Wait until oThread finishes. Join also has overloads
// that take a millisecond interval or a TimeSpan object.
oThread.Join();

Console.WriteLine();
Console.WriteLine("Alpha.Beta has finished");

try
{
Console.WriteLine("Try to restart the Alpha.Beta thread");
oThread.Start();
}
catch (ThreadStateException)
{
Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
Console.WriteLine("Expected since aborted threads cannot be restarted.");
}
return 0;
}
}
boytomato 2004-06-17
  • 打赏
  • 举报
回复
//版权所有 (C) 2000 Microsoft Corporation。保留所有权利。

// 线程\Mutex.cs
//
// 若要从命令行生成此示例,请调用
//
// csc Mutex.cs
//

using System;
using System.Threading;

public class MutexSample
{
static Mutex gM1;
static Mutex gM2;
const int ITERS = 100;
static AutoResetEvent Event1 = new AutoResetEvent(false);
static AutoResetEvent Event2 = new AutoResetEvent(false);
static AutoResetEvent Event3 = new AutoResetEvent(false);
static AutoResetEvent Event4 = new AutoResetEvent(false);

public static void Main(String[] args)
{
Console.WriteLine("Mutex Sample ...");
// 创建 Mutex initialOwned,名称为“MyMutex”。
gM1 = new Mutex(true,"MyMutex");
// 创建没有名称的 Mutex initialOwned。
gM2 = new Mutex(true);
Console.WriteLine(" - Main Owns gM1 and gM2");

AutoResetEvent[] evs = new AutoResetEvent[4];
evs[0] = Event1; // t1 的事件
evs[1] = Event2; // t2 的事件
evs[2] = Event3; // t3 的事件
evs[3] = Event4; // t4 的事件

MutexSample tm = new MutexSample( );
Thread t1 = new Thread(new ThreadStart(tm.t1Start));
Thread t2 = new Thread(new ThreadStart(tm.t2Start));
Thread t3 = new Thread(new ThreadStart(tm.t3Start));
Thread t4 = new Thread(new ThreadStart(tm.t4Start));
t1.Start( ); // 执行 Mutex.WaitAll(gM1 和 gM2 的 Mutex[])
t2.Start( ); // 执行 Mutex.WaitOne(Mutex gM1)
t3.Start( ); // 执行 Mutex.WaitAny(gM1 和 gM2 的 Mutex[])
t4.Start( ); // 执行 Mutex.WaitOne(Mutex gM2)

Thread.Sleep(2000);
Console.WriteLine(" - Main releases gM1");
gM1.ReleaseMutex( ); // t2 和 t3 将结束并发出信号

Thread.Sleep(1000);
Console.WriteLine(" - Main releases gM2");
gM2.ReleaseMutex( ); // t1 和 t4 将结束并发出信号

// 等待直到所有这 4 个线程都发出信号表明它们已完成。
WaitHandle.WaitAll(evs);
Console.WriteLine("... Mutex Sample");
}

public void t1Start( )
{
Console.WriteLine("t1Start started, Mutex.WaitAll(Mutex[])");
Mutex[] gMs = new Mutex[2];
gMs[0] = gM1; // 为 WaitAll 调用创建并加载 Mutex 的数组
gMs[1] = gM2;
Mutex.WaitAll(gMs); // 等待直到 gM1 和 gM2 都已释放
Thread.Sleep(2000);
Console.WriteLine("t1Start finished, Mutex.WaitAll(Mutex[]) satisfied");
Event1.Set( ); // AutoResetEvent.Set() 标记方法已完成
}

public void t2Start( )
{
Console.WriteLine("t2Start started, gM1.WaitOne( )");
gM1.WaitOne( ); // 等待直到 Mutex gM1 被释放
Console.WriteLine("t2Start finished, gM1.WaitOne( ) satisfied");
Event2.Set( ); // AutoResetEvent.Set() 标记方法已完成
}

public void t3Start( )
{
Console.WriteLine("t3Start started, Mutex.WaitAny(Mutex[])");
Mutex[] gMs = new Mutex[2];
gMs[0] = gM1; // 为 WaitAny 调用创建并加载 Mutex 的数组
gMs[1] = gM2;
Mutex.WaitAny(gMs); // 等待直到任何一个 Mutex 被释放
Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])");
Event3.Set( ); // AutoResetEvent.Set() 标记方法已完成
}

public void t4Start( )
{
Console.WriteLine("t4Start started, gM2.WaitOne( )");
gM2.WaitOne( ); // 等待直到 Mutex gM2 被释放
Console.WriteLine("t4Start finished, gM2.WaitOne( )");
Event4.Set( ); // AutoResetEvent.Set() 标记方法已完成
}
}

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧