引用:
using System.Text;
using System.Threading.Tasks;
using System.Threading;
定义方法:
public static void method()
{
Console.WriteLine("线程名称1:{0}",Thread.CurrentThread.Name.ToString());
Console.WriteLine("线程优先级1:{0}", Thread.CurrentThread.Priority.ToString());
//string state;
for (int i = 1; i < 100; i++)
{
// //state = Thread.CurrentThread.ThreadState.ToString();
if (i % 40 == 0)
{
//Console.WriteLine(i.ToString()+"当前的线程状态:{0}",state);
Console.WriteLine(">");
}
else
{
Console.WriteLine("<");
}
//else
//Console.WriteLine(i.ToString() + " ");
}
}
定义方法:
public static void method1()
{
Console.WriteLine("线程名称2:{0}", Thread.CurrentThread.Name.ToString());
Console.WriteLine("线程优先级2:{0}", Thread.CurrentThread.Priority.ToString());
for (int i = 1; i < 100; i++)
{
// //state = Thread.CurrentThread.ThreadState.ToString();
if (i % 40 == 0)
{
//Console.WriteLine(i.ToString()+"当前的线程状态:{0}",state);
Console.WriteLine("12");
}
else
{
Console.WriteLine("34");
}
}
}
线程操作:
static void Main(string[] args)
{
ThreadStart ts1 = new ThreadStart(method);
ThreadStart ts2 = new ThreadStart(method1);
Thread t1 = new Thread(ts1);
Thread t2 = new Thread(ts2);
t1.Name = "我要自诩";
t2.Name = "我要自诩2";
t1.Priority = ThreadPriority.Highest;
t2.Priority = ThreadPriority.Highest;
Console.WriteLine("线程开始启动");
t1.Start();
while (t1.IsAlive)
{
Console.WriteLine("线程1被挂起");
t1.Suspend();
Thread.Sleep(10);
t1.Resume();
Console.WriteLine("线程1重新启动");
if (!t1.IsAlive)
{
t2.Start();
while(t2.IsAlive)
{
Console.WriteLine("线程2被挂起");
t2.Suspend();
Thread.Sleep(30);
t2.Resume();
//t2.Abort();
Console.WriteLine("线程2重新启动");
}
}
}
//int i = 0;
//while(t.IsAlive)
//{
// i++;
// if (i == 5)
// {
// Thread.Sleep(5);
// //t.Suspend();
// t.Abort();
// Console.WriteLine("线程被终止");
// //Thread.Sleep(600);
// //t.Resume();
// }
// else
// {
// Console.WriteLine("线程重被挂起");
// t.Suspend();
// Thread.Sleep(500);
// t.Resume();
// Console.WriteLine("线程重新启动");
// }
//}
Console.ReadLine();
}