namespace Test
{
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(TestMethod));
Thread t2 = new Thread(new ParameterizedThreadStart(TestMethod));
t1.IsBackground = true;
t2.IsBackground = true;
t1.Start();
t2.Start("hello");
Console.ReadKey();
}
public static void TestMethod()
{
Console.WriteLine("不带参数的线程函数");
}
public static void TestMethod(object data)
{
string datastr = data as string;
Console.WriteLine("带参数的线程函数,参数为:{0}", datastr);
}
}
}
class Program
{
static void Main(string[] args)
{
//将工作项加入到线程池队列中,这里可以传递一个线程参数
ThreadPool.QueueUserWorkItem(TestMethod, "Hello");
Console.ReadKey();
}
public static void TestMethod(object data)
{
string datastr = data as string;
Console.WriteLine(datastr);
}
}
class Program
{
static void Main(string[] args)
{
Task<Int32> t = new Task<Int32>(n => Sum((Int32)n), 1000);
t.Start();
t.Wait();
Console.WriteLine(t.Result);
Console.ReadKey();
}
private static Int32 Sum(Int32 n)
{
Int32 sum = 0;
for (; n > 0; --n)
checked{ sum += n;} //结果太大,抛出异常
return sum;
}
}
class Program
{
static void Main(string[] args)
{
Task<Int32> t = new Task<Int32>(n => Sum((Int32)n), 1000);
t.Start();
//t.Wait();
Task cwt = t.ContinueWith(task => Console.WriteLine("The result is {0}",t.Result));
Console.ReadKey();
}
private static Int32 Sum(Int32 n)
{
Int32 sum = 0;
for (; n > 0; --n)
checked{ sum += n;} //结果溢出,抛出异常
return sum;
}
}
public delegate string MyDelegate(object data);
class Program
{
static void Main(string[] args)
{
MyDelegate mydelegate = new MyDelegate(TestMethod);
IAsyncResult result = mydelegate.BeginInvoke("Thread Param", TestCallback, "Callback Param");
//异步执行完成
string resultstr = mydelegate.EndInvoke(result);
}
//线程函数
public static string TestMethod(object data)
{
string datastr = data as string;
return datastr;
}
//异步回调函数
public static void TestCallback(IAsyncResult data)
{
Console.WriteLine(data.AsyncState);
}
}
class Program
{
static int counter = 1;
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(F1));
Thread t2 = new Thread(new ThreadStart(F2));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
System.Console.ReadKey();
}
static void F1()
{
for (int i = 0; i < 5; i++)
{
Interlocked.Increment(ref counter);
System.Console.WriteLine("Counter++ {0}", counter);
Thread.Sleep(10);
}
}
static void F2()
{
for (int i = 0; i < 5; i++)
{
Interlocked.Decrement(ref counter);
System.Console.WriteLine("Counter-- {0}", counter);
Thread.Sleep(10);
}
}
}
public void MonitorSomeThing()
{
try
{
Monitor.Enter(obj);
dosomething();
}
catch(Exception ex)
{
}
finally
{
Monitor.Exit(obj);
}
}
class SynchronizedCache
{
private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
private Dictionary<int, string> innerCache = new Dictionary<int, string>();
public string Read(int key)
{
cacheLock.EnterReadLock();
try
{
return innerCache[key];
}
finally
{
cacheLock.ExitReaderLock();
}
}
public void Add(int key, string value)
{
cacheLock.EnterWriteLock();
try
{
innerCache.Add(key, value);
}
finally
{
cacheLock.ExitWriteLock();
}
}
public bool AddWithTimeout(int key, string value, int timeout)
{
if (cacheLock.TryEnterWriteLock(timeout))
{
try
{
innerCache.Add(key, value);
}
finally
{
cacheLock.ExitReaderLock();
}
return true;
}
else
{
return false;
}
}
public AddOrUpdateStatus AddOrUpdate(int key, string value)
{
cacheLock.EnterUpgradeableReadLock();
try
{
string result = null;
if (innerCache.TryGetValue(key, out result))
{
if (result == value)
{
return AddOrUpdateStatus.Unchanged;
}
else
{
cacheLock.EnterWriteLock();
try
{
innerCache[key] = value;
}
finally
{
cacheLock.ExitWriteLock();
}
return AddOrUpdateStatus.Updated;
}
}
else
{
cacheLock.EnterWriteLock();
try
{
innerCache.Add(key, value);
}
finally
{
cacheLock.ExitWriteLock();
}
return AddOrUpdateStatus.Added;
}
}
finally
{
cacheLock.ExitUpgradeableReadLock();
}
}
public void Delete(int key)
{
cacheLock.EnterWriteLock();
try
{
innerCache.Remove(key);
}
finally
{
cacheLock.ExitWriteLock();
}
}
public enum AddOrUpdateStatus
{
Added,
Updated,
Unchanged
};
}
public Thread thrd;
//创建一个可授权2个许可证的信号量,且初始值为2
static Semaphore sem = new Semaphore(2, 2);
public mythread(string name)
{
thrd = new Thread(this.run);
thrd.Name = name;
thrd.Start();
}
void run()
{
Console.WriteLine(thrd.Name + "正在等待一个许可证……");
//申请一个许可证
sem.WaitOne();
Console.WriteLine(thrd.Name + "申请到许可证……");
for (int i = 0; i < 4 ; i++)
{
Console.WriteLine(thrd.Name + ": " + i);
Thread.Sleep(1000);
}
Console.WriteLine(thrd.Name + " 释放许可证……");
//释放
sem.Release();
}
}
class mysemaphore
{
public static void Main()
{
mythread mythrd1 = new mythread("Thrd #1");
mythread mythrd2 = new mythread("Thrd #2");
mythread mythrd3 = new mythread("Thrd #3");
mythread mythrd4 = new mythread("Thrd #4");
mythrd1.thrd.Join();
mythrd2.thrd.Join();
mythrd3.thrd.Join();
mythrd4.thrd.Join();
}
}
class Test
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
bool flag = false;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, "Test", out flag);
//第一个参数:true--给调用线程赋予互斥体的初始所属权
//第一个参数:互斥体的名称
//第三个参数:返回值,如果调用线程已被授予互斥体的初始所属权,则返回true
if (flag)
{
Console.Write("Running");
}
else
{
Console.Write("Another is Running");
System.Threading.Thread.Sleep(5000);//线程挂起5秒钟
Environment.Exit(1);//退出程序
}
Console.ReadLine();
}
}
static void Main(string[] args)
{
string MutexName = "InterProcessSyncName";
Mutex SyncNamed; //声明一个已命名的互斥对象
try
{
SyncNamed = Mutex.OpenExisting(MutexName); //如果此命名互斥对象已存在则请求打开
}
catch (WaitHandleCannotBeOpenedException)
{
SyncNamed = new Mutex(false, MutexName); //如果初次运行没有已命名的互斥对象则创建一个
}
Task MulTesk = new Task
(
() => //多任务并行计算中的匿名方法,用委托也可以
{
for (; ; ) //为了效果明显而设计
{
Console.WriteLine("当前进程等待获取互斥访问权......");
SyncNamed.WaitOne();
Console.WriteLine("获取互斥访问权,访问资源完毕,按回车释放互斥资料访问权.");
Console.ReadLine();
SyncNamed.ReleaseMutex();
Console.WriteLine("已释放互斥访问权。");
}
}
);
MulTesk.Start();
MulTesk.Wait();
}
Parallel.For(0, 1000000, i =>
{
Stopwatch sw1 = new Stopwatch();
sw1.Start();
if (redisHelper.GetRedisOperation().Lock(key))
{
var tt = int.Parse(redisHelper.GetRedisOperation().StringGet("calc"));
tt++;
redisHelper.GetRedisOperation().StringSet("calc", tt.ToString());
redisHelper.GetRedisOperation().UnLock(key);
}
var v = sw1.ElapsedMilliseconds;
if (v >= 10 * 1000)
{
Console.Write("f");
}
sw1.Stop();
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有