Program.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Base14
{
class Program
{
static void Main(string[] args)
{
//Thread.CurrentThread.Name = "当前运行的线程";
//Console.WriteLine(Thread.CurrentThread.Name);
//Console.WriteLine(Thread.CurrentThread.IsBackground);
//Console.ReadKey();
//Console.WriteLine(Process.GetCurrentProcess().ProcessName);
//Console.ReadKey();
//显示任务资源管理器的进程
//foreach (Process item in Process.GetProcesses())
//{
// if (item.ProcessName.Contains("ie"))
// {
// Console.WriteLine(item.ProcessName);
// }
//}
//Console.ReadKey();
//网址进程进入
// Process.Start("iexplore", "http://www.baidu.com");
//委托
//ThreadStart ts = new ThreadStart(show);
//Thread myThread = new Thread(ts);
//myThread.Name = "newThread";
启动
//myThread.Start();
终止
myThread.Abort();
挂起 先不执行
//myThread.Suspend();
//Console.WriteLine("哈哈哈哈");
唤醒 执行线程
//myThread.Resume();
//Console.ReadKey();
//执行线程
//Thread mythread = new Thread(show);
//mythread.Start();
//Console.ReadKey();
//Thread mythread = new Thread(ThreadMethod);
//mythread.Start();
//Thread.Sleep(2000);
//mythread.Abort();
//Console.ReadKey();
//Thread mt = new Thread(ThreadMethod);
//mt.Start("我是一个参数");
//Console.ReadKey();
//Console.WriteLine(Thread.GetDomain().FriendlyName);
//Console.WriteLine(Thread.GetDomainID());
//Console.ReadKey();
//中断
//Thread mt = new Thread(ThreadMethod);
//mt.Start();
//Thread.Sleep(10);
//mt.Interrupt();
//Console.ReadKey();
//Book book = new Book();
//Thread t1 = new Thread(book.Sale);
//t1.Name = "第一个线程";
//Thread t2 = new Thread(book.Sale);
//t2.Name = "第二个线程";
//t1.Start();
//t2.Start();
//Console.ReadKey();
for (int i = 0; i < 5; i++)
{
Thread thread = new Thread(MyReadom);
thread.Start();
}
Console.ReadKey();
}
public static void MyReadom()
{
byte[] bytes = Guid.NewGuid().ToByteArray();
int index = BitConverter.ToInt32(bytes, 0);
if (index < 0)
{
//递归 自己调用自己
MyReadom();
}
else
{
Random r = new Random();
Console.WriteLine(r.Next(index)/10000);
}
//Random r = new Random();
//int index = r.Next(0, 99);
//Console.WriteLine(index);
}
//public static void ThreadMethod(object objects)
//{
// try
// {
// for (int i = 0; i < 10; i++)
// {
// //Thread.Sleep(500);
// Console.WriteLine(objects);
// }
// }
// catch (Exception e)
// {
// Console.WriteLine("执行的线程被强制终止");
// }
//}
//public static void ThreadMethod()
//{
// try
// {
// for (int i = 0; i < 10; i++)
// {
// Thread.Sleep(500);
// Console.WriteLine(i);
// }
// }
// catch (Exception e)
// {
// Console.WriteLine("执行的线程被强制终止");
// }
//}
//public static void show()
//{
// Console.WriteLine("独立休眠5秒");
// Thread.Sleep(5000);
//}
}
}
Book.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Base14
{
class Book
{
public int sum = 100;
public void Sale()
{
//this 哪一个线程 在调用 就是哪一个线程
//lock (this)
//{
Thread.Sleep(1000);
if (sum == 0)
{
Console.WriteLine("书本已售罄");
return;
}
while (sum > 0)
{
Console.WriteLine("卖出第{0}本书,谁买的{1}", sum, Thread.CurrentThread.Name);
sum -= 1;
}
//}
}
}
}