Different __java in Java in yield () and join () threads

Source: Internet
Author: User

The understanding of thread priority

The understanding of the thread's priority is very helpful to the understanding of multithreading and the yield () working principle.

1. If you do not specify a priority, then all thread priorities are of the normal priority level.
2, the priority is divided into 1 to 10 of these 10 levels, where 10 is the highest priority, 1 is the lowest priority, 5 is the normal priority.
3. A thread with the highest priority will be executed first, but this does not guarantee that it is running at startup.
4. For threads waiting to be scheduled in a thread pool, the currently executing thread may have a higher priority.
5, decide which thread should be executed is the thread dispatcher.
6, T.setpriority () method can be used to set the priority of the thread.
7, the priority of the thread should be set before the Start method call of the threads.
8, you can use constant min_priority,max_priority and norm_priority to set priorities.

yield () method

The call to the yield method means that the thread tells the virtual machine it is willing to allow other threads to be dispatched in this place, that is, it can give up the current execution of the time slice, but it does not cause the current thread blocking, the next time slice it may be continued execution, which indicates that the thread is not doing something urgent. Note that this is only a hint and is not guaranteed to have no effect.

The following is the definition of yield () in Thread.java

/** * A hint to the scheduler, the current thread are willing to yield their current use of
  A processor. The scheduler is free to ignore
  * this hint. Yield is a heuristic attempt to improve relative progression between threads that would otherwise a CPU.
  * Its use should is combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.
  */public

static native void yield ();

There are important points to follow:

1. Yield is a static native (native) method
2. Yield tells the currently executing thread to give an opportunity to a thread in the thread pool that has the same priority, so that they are scheduled.
3. Yield does not guarantee that the currently running thread can be quickly converted to a running state
4, it can only make a thread from the run state to the operational state, rather than waiting or blocking state

In the following sample program, I randomly created two threads called producers and consumers. The producer is set to the minimum priority, and the consumer is set to the highest priority level. I will run the program separately in the case of Thread.yield () and not annotated. The yield () method is not invoked, although the output sometimes changes, but usually the consumer line first prints out and then the matter producer.

Package test.core.threads;

public class Yieldexample
{public
   static void Main (string[] args)
   {
      Thread producer = new producer (); C5/>thread consumer = new Consumer ();

      Producer.setpriority (thread.min_priority); Min Priority
      consumer.setpriority (thread.max_priority);//max Priority

      Producer.start ();
      Consumer.start ();
   }

Class Producer extends Thread
{public
   void run ()
   {for
      (int i = 0; i < 5; i++)
      {
         System. Out.println ("I am producer:produced Item" + i);
         Thread.yield ();

}} Class Consumer extends Thread
{public
   void run ()
   {for
      (int i = 0; i < 5; i++)
      {
         System. Out.println ("I am consumer:consumed Item" + i);
         Thread.yield ();}}

Output results without yield () method:

I am consumer:consumed item 0
 I am consumer:consumed item 1
 I am consumer:consumed Item 2
 I am Consumer : Consumed Item 3
 I am consumer:consumed Item 4
 I am producer:produced item 0
 I am producer:produced Ite M 1
 I am producer:produced Item 2
 I am producer:produced Item 3
 I am producer:produced Item 4

Output results with yield () method:

I am producer:produced item 0
 I am consumer:consumed item 0
 I am producer:produced item 1
 I am consume R:consumed Item 1
 I am producer:produced Item 2
 I am consumer:consumed Item 2
 I am producer:produce D Item 3
 I am consumer:consumed Item 3
 I am producer:produced Item 4
 I am consumer:consumed Item 4

Join () method

A join method of a thread instance can add execution of one thread to another thread so that the current thread can run only after the joined thread has finished executing. If the Join method is invoked by an instance thread, then the thread is blocked until the execution of the instance threads completes.

Waits for this thread to die.

Public final void Join () throws Interruptedexception

Setting a timeout within the join () method causes the effect of the join () method to be invalid after a specific timeout. When a timeout occurs, the main method and the task thread are equal when the application runs. However, when sleep is involved, the join () method relies on the operating system to clock, so you should not assume that the join () method will wait for the time you specify.

Package test.core.threads;

public class Joinexample
{public
   static void Main (string[] args) throws Interruptedexception
   {
      Thread t = new Thread (new Runnable ()
         {public
            void run ()
            {
               System.out.println ("A-task started");
               System.out.println ("Sleeping for 2 Seconds");
               Try
               {
                  thread.sleep;
               } catch (Interruptedexception e)
               {
                  e.printstacktrace ();
               }
               SYSTEM.OUT.PRINTLN ("task completed");
            }
         );
      thread T1 = new Thread (new Runnable ()
         {public
            void run ()
            {
               System.out.println ("Second task Completed ");
            }
         ;
      T.start (); Line
      t.join ();/[Line
      T1.start ();
   }
}

Output results:

The started
sleeping for 2 seconds the
task completed Second tasks completed

Original link: difference between yield () and join () in 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.