Part 92 Significance of Thread Join and Thread IsAlive functions, threadisalive

Source: Internet
Author: User

Part 92 Significance of Thread Join and Thread IsAlive functions, threadisalive

Thread. Join & Thread. IsAlive functions

Join blocks the current thread and makes it wait until the thread on which Join method is invoked completes. join method also has a overload where we can specify the timeout. if we don't specify the timeout the calling thread waits indefinitely (indefinite), until the thread on which Join () is invoked completes. this overloaded Join (int millisecondesTimeout) method returns boolean. true if the thread has terminated (terminated) otherwise false.

Join is special (especially) useful when we need to wait and collect result from a thread execution or if we need to do some clean-up after the thread has completed.

IsAlive returns boolean. True if the thread is still executing otherwise false.

public static void Main(string[] args)        {            Console.WriteLine("main start");            Thread t1 = new Thread(ThreadFunction1);            t1.Start();            Thread t2 = new Thread(ThreadFunction2);            t2.Start();            if(t1.Join(1000))            {                Console.WriteLine("threadfunction1 end");            }            else            {                Console.WriteLine("threadfunction1 still working");            }            if(t1.IsAlive)            {                for (int i = 1; i < 10; i++)                {                    Console.WriteLine("threadfunction1 still working...");                    Thread.Sleep(500);                                    }            }            Console.WriteLine("main end");        }        public static void ThreadFunction1()        {            Console.WriteLine("threadfunction1 start");            Thread.Sleep(5000);            Console.WriteLine("threadfunction1 end");        }        public static void ThreadFunction2()        {            Console.WriteLine("threadfunction2 start");        }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.