程序运行代码如下:
 public class ThreadInterrupt
    {
        public static Thread sleeper;
        public static Thread worker;        public static void Main()
        {
            Console.WriteLine("Entering the void Main!");
            sleeper = new Thread(new ThreadStart(SleepingThread));
            worker = new Thread(new ThreadStart(AwakeTheThread));            sleeper.Start();
            worker.Start();
           
            Console.WriteLine("Exiting the void Main!");
        }
        public static void SleepingThread()
        {
            Console.WriteLine("Entering the SleepingThread!");
            for (int i = 0; i < 30; i++)
            {
                Console.WriteLine(i + "");
                if (i == 10 || i == 20 || i == 30)
                {
                    Console.WriteLine("Going to sleep at:" + i);
                    Thread.Sleep(20);
                 }
            }
            Console.WriteLine("Exiting the SleepingThread!");
        }        public static void AwakeTheThread()
        {
            Console.WriteLine("Entering the AwakeTheThread!");
            for (int i = 31; i < 100; i++)
            {
                Console.WriteLine(i + "");
                Console.WriteLine(sleeper.ThreadState.ToString());
                if (sleeper.ThreadState == System.Threading.ThreadState.WaitSleepJoin)
                {
                    Console.WriteLine("Interrupting the sleeping thread");
                    sleeper.Interrupt();
                }
            }
            Console.WriteLine("Exiting the AwakeTheThread!");
        }
    }为什么在AwakeTheThread()方法唤醒sleeper线程后,sleeper线程在执行Thread.Sleep(20);时抛线程已从等待状态中断的异常?请各位帮忙指点。100分相送

解决方案 »

  1.   

    我在网上查的资料说Interrupt() 这个方法是防止线程进入WaitSleepJoin状态的,一旦使用,你后面再用sleep,wait 等方法就会抛异常。
      

  2.   

    我在网上查的资料说Interrupt() 这个方法是防止线程进入WaitSleepJoin状态的,一旦使用,你后面再用sleep,wait 等方法就会抛异常。
      

  3.   

    当线程进入睡眠时,它就进入WaitSleepJoin状态。如果线程处于睡眠状态,在到达指定的睡眠时间之前唤醒线程的唯一方法是使用Interrupt()方法,Interrupt()方法会将线程放回到调度队列中。
    ======
    既然线程已经唤醒 其ThreadState应该是Running,那么此时再Sleep应该没有问题吧?
      

  4.   


    你可以看下这篇文章,和你说的作用不一样。
    http://topic.csdn.net/u/20070703/15/043cf41f-7738-4d37-b7d5-f29cbda2b0fe.html
      

  5.   

    http://msdn.microsoft.com/zh-cn/events/tttdef8x(VS.80).aspx
    System.Threading.Thread.Interrupt 将线程从它可能处于的任何等待中唤醒,并在目标线程中引发 ThreadInterruptedException。
      

  6.   

    我也非常想知道waitsleepjoin状态下的线程如何唤醒后并挂起他。期待高人回答