是否有可能在多个线程之间挂起一个线程,然后恢复该挂起的线程.... ?????? [英] is it possible to suspend a thread among multiple threds,and resume that suspended thread....??????

查看:97
本文介绍了是否有可能在多个线程之间挂起一个线程,然后恢复该挂起的线程.... ??????的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多线程中,可以通过使用用户定义的线程名称来恢复派生类中的挂起线程.

我的程序可以让我给正确的代码

使用系统;
 使用System.Collections.Generic;
 使用System.Linq;
 使用System.Text;
 使用System.Threading;

 命名空间tsuspend
 {
     类线程
     {
         
         内部空隙m1()
         {
             
             对于(int i = 0; i< 10; i ++)
             {
                 Console.WriteLine(Thread.CurrentThread.Name +"=" +(i + 1));

                 如果(i == 5)
                 {
                     Console.WriteLine(Thread.CurrentThread.Name +将要挂起");
                     Thread.CurrentThread.Suspend();
                 }
                 否则(i> 5)
                 {
                     Thread.CurrentThread.Resume();
                 }
                
             }
         }
     }
     班级计划
     {
         静态void Main(string [] args)
         {
            线程thr =新线程();
             线程thr1 =新线程(new ThreadStart(thr.m1));
             线程thr2 =新线程(新ThreadStart(thr.m1));
             thr1.Name =詹姆斯";
             thr2.Name ="raj";
             thr1.Start();
             thr2.Start();
            
        
             Console.ReadLine();
         }
     }
 } 


================================================

我想在下面输出

================================================

JAMES = 1

JAMES = 2

JAMES = 3

JAMES = 4

JAMES = 5

詹姆斯被暂停

RAJ = 1

RAJ = 2

RAJ = 3

RAJ = 4

RAJ = 5

RAJ被暂停

JAMES = 6

JAMES = 7

JAMES = 8

JAMES = 9

JAMES = 10

RAJ = 6

RAJ = 7

RAJ = 8

RAJ = 9

RAJ = 10

=====================================

感谢您耐心地帮助我实现这一目标

谢谢您

解决方案

课程
{

    静态void Main(string [] args)
    {
        线程thr =新线程();
        线程thr1 =新线程(new ThreadStart(thr.m1));
        线程thr2 =新线程(新ThreadStart(thr.m1));
        thr1.Name ="james";
        thr2.Name ="raj";

        thr1.Start();
        thr2.Start();

        一会儿(true)
        {
            如果(thr1.ThreadState == ThreadState.Suspended)
            {
                thr1.Resume();
                休息;
            }
        }

        一会儿(true)
        {
            如果(thr2.ThreadState == ThreadState.Suspended)
            {
                thr2.Resume();
                休息;
            }
        }


        Console.ReadLine();
    }
}

类线程
{
    内部空隙m1()
    {

        对于(int i = 0; i< 10; i ++)
        {
            Console.WriteLine(Thread.CurrentThread.Name +"=" +(i +1));

            如果(i == 4)
            {
                Console.WriteLine(Thread.CurrentThread.Name +将要挂起"));
                Thread.CurrentThread.Suspend();


            }
            //否则(i> 5)
            //{
            //Thread.CurrentThread.Resume();
            //}

        }
    }
} 

如果要具有严格的输出顺序,则应使用线程事件进行控制和同步.

谢谢

                                                


In multiple threading is it possible to resume a suspended thread in derived class by using user defined thread name....?

MY PROGRAM IS THIS CAN U PLZ GIVE ME CORRECT CODE

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading;

 namespace tsuspend
 {
     class thread
     {
         
         internal void m1()
         {
             
             for (int i = 0; i < 10; i++)
             {
                 Console.WriteLine(Thread.CurrentThread.Name+"="+(i+1));

                 if (i == 5)
                 {
                     Console.WriteLine(Thread.CurrentThread.Name+" is going to suspend");
                     Thread.CurrentThread.Suspend();  
                 }
                 else if (i > 5)
                 {
                     Thread.CurrentThread.Resume();
                 }
                
             }
         }
     }
     class Program
     {
         static void Main(string[] args)
         {
            thread thr = new thread();
             Thread thr1 = new Thread(new ThreadStart(thr.m1));
             Thread thr2 = new Thread(new ThreadStart(thr.m1));
             thr1.Name="james";
             thr2.Name = "raj";
             thr1.Start();
             thr2.Start();
            
        
             Console.ReadLine();
         }
     }
 }


===============================================

I WANT OUTPUT LIKE BELOW

===============================================

JAMES=1

JAMES=2

JAMES=3

JAMES=4

JAMES=5

JAMES IS SUSPENDED

RAJ=1

RAJ=2

RAJ=3

RAJ=4

RAJ=5

RAJ IS SUSPENDED

JAMES=6

JAMES=7

JAMES=8

JAMES=9

JAMES=10

RAJ=6

RAJ=7

RAJ=8

RAJ=9

RAJ=10

=======================================

THANK YOU FOR PATIENTIE PLZ HELP ME TO ACHIVE THIS

THANK YOU IN ADVANCE

解决方案

class Program
{

    static void Main(string[] args)
    {
        thread thr = new thread();
        Thread thr1 = new Thread(new ThreadStart(thr.m1));
        Thread thr2 = new Thread(new ThreadStart(thr.m1));
        thr1.Name = "james";
        thr2.Name = "raj";

        thr1.Start();
        thr2.Start();

        while (true)
        {
            if (thr1.ThreadState == ThreadState.Suspended)
            {
                thr1.Resume();
                break;
            }
        }

        while (true)
        {
            if (thr2.ThreadState == ThreadState.Suspended)
            {
                thr2.Resume();
                break;
            }
        }


        Console.ReadLine();
    }
}

class thread
{
    internal void m1()
    {

        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(Thread.CurrentThread.Name + "=" + (i + 1));

            if (i == 4)
            {
                Console.WriteLine(Thread.CurrentThread.Name + " is going to suspend");
                Thread.CurrentThread.Suspend();


            }
            //else if (i > 5)
            //{
            //    Thread.CurrentThread.Resume();
            //}

        }
    }
}

if you want to have strict output order, you should use thread event to control and synchronize.

Thanks

                                                


这篇关于是否有可能在多个线程之间挂起一个线程,然后恢复该挂起的线程.... ??????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆