Control of Threads in Java

Source: Internet
Author: User

Control of the thread:

Threading Support in Java provides some handy tools to better control the execution of threads through these handy ways of working.

One, join thread:

Thread provides a way for a thread to wait for another thread to complete-thejoin() method. When another thread's join method is called in a program execution stream , the calling thread is blocked until The join joined by the Join method Until the thread finishes executing.

The Join () method has the following 3 overloaded forms:

1. Join(): Waits for The thread of the join to finish executing.

2, join (Long Millis): wait for the join thread for the longest time of Millis milliseconds, if the Millis in milliseconds is Join thread has not finished executing, it will no longer wait.

3. Join(long millis, int nanos): the thread waiting for the join is the longest time Millis milliseconds plus Nanos nanoseconds.

1  Public classJointhreadextendsthread{2     Private inti;3      Public voidrun () {4          for(; i<30;i++){5             Try{Sleep (1000);6SYSTEM.OUT.PRINTLN ("Thread's Name:" +getname () + "" "+i);}Catch(Exception e) {7 System.out.println (e);8             }    9         }Ten     } One      Public Static voidMain (string[] args) { AJointhread j=NewJointhread (); -              for(intk=0;k<100;k++){ -                 Try{ theThread.Sleep (1000); -System.out.println ("The name of the current thread is:" +thread.currentthread (). GetName () + "" +k); -}Catch(Exception e) { - System.out.println (e); +                 } -                 if(k==5){ + J.start (); A                 } at                 if(k==10){ -                     Try{J.join ();}Catch(Exception e) { - System.out.println (e); -                     } -                 } -             } in         } -}

Second, the interrupt method in the disconnection process:

The Stop() method is also a way to break threads in, but it has been deprecated.

The interrupt method can be used to request a terminating thread.

When the interrupt method is called on a thread , the interrupt state of the thread is set. At this point each thread has a boolean flag that each thread should check periodically to determine whether the thread is being terminated.

The interrupted program can decide how to respond to interrupts.

Third, the thread sleeps : Sleep

If you want the currently executing thread to pause for a while and go into a blocking state, you can do so by calling the static sleep () method of the thread class .

When a thread calls the sleep () method into a blocking state, the threads do not get an opportunity to execute during their sleep period, even if there are no other executable threads in the system, and the thread in sleep will not execute. Therefore , the sleep method is often used to pause the execution of the program.

Iv. the concession of the thread:yield

The yield method is a somewhat similar method to the Sleep method, which is also A static method provided by the thread class, which also allows the currently executing thread to pause. But it does not block the thread, he just makes the thread in the ready state. Yield The method simply pauses the current thread and allows the system's thread scheduler to reschedule.

Summarize The difference between the yield and sleep methods:

1. When the Sleep method is paused but the front-thread is executed, the opportunity is given to other threads, ignoring the priority of the other threads, but the yield method will only give the same priority or higher priority thread execution opportunity.

2, the Sleep method will put the thread into a blocking state, know that the blocking time to go to the ready state, and the yield method does not put the thread into a blocking state, he just force the current thread into a ready state.

3. the Sleep method throws an exception, and the yield method does not throw an exception.

Control of Threads in Java

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.