Detailed analysis of all methods in Java multithreading technology

Source: Internet
Author: User
1. Run () and start ()

These two methods should be familiar with each other. Put the code to be processed in parallel in the run () method, and the start thread of the START () method will automatically call the run () method, this is defined by the Java memory mechanism. The run () method must have the public access permission, and the return value type is void.

Ii. Keywords synchronized

This keyword is used to protect shared data. The premise is to distinguish which data is shared data. Each object has a lock sign. When a thread accesses this object, the data modified by synchronized will be "locked" to prevent other threads from accessing it. After the current thread accesses this part of data, the lock flag is released and other threads can access the data.

 public ThreadTest implements Runnable
  {
  public synchronized void run(){
  for(int i=0;i<10;i++)
  {
  System.out.println(" " + i);
  }
  }
  public static void main(String[] args)
  {
  Runnable r1 = new ThreadTest();
  Runnable r2 = new ThreadTest();
  Thread t1 = new Thread(r1);
  Thread t2 = new Thread(r2);
  t1.start();
  t2.start();
  }
  }

The I variable in the above section does not share data, that is, the synchronized keyword does not work here. Because t1 and t2 threads are the threads of two objects (R1, R2. The data of different objects is different, so I variables of R1 and R2 are not shared data.

It takes effect only when the code is changed to the following: synchronized keyword.

Runnable r = new threadtest ();

Thread T1 = new thread (R );

Thread t2 = new thread (R );

T1.start ();

T2.start ();

Iii. Sleep ()

Pause the execution of the current thread (that is, the thread that calls this method) for a period of time, so that other threads can continue to execute, but it does not release the object lock. That is, if there is a synchronized synchronization block, other threads still access Shared data differently. Note that this method must capture exceptions.

 
For example, if two threads run simultaneously (without synchronized), one thread has a priority of max_priority and the other is min_priority. If no
Sleep () method. A thread with a higher priority can be executed only after the thread with a higher priority is completed. However, when a thread with a higher priority is sleep (5000, low priority will give you the opportunity to execute.

In short, sleep () can give low-priority threads a chance to execute. Of course, it can also give threads with the same priority and higher priority a chance to execute.

4. Join ()

The join () method enables the thread that calls this method to complete execution before that, that is, wait until the thread that calls this method finishes execution before continuing execution. Note that this method also needs to capture exceptions.

V. Yield ()

Similar to sleep (), the yield () method can only give threads with the same priority a chance to execute.

6. Wait (), Policy (), and policyall ()

 
These three methods are used to coordinate multiple threads to access Shared data. Therefore, these three methods must be used in the synchronized statement block. We have mentioned the key of synchronized.
Word is used to protect shared data and prevent other threads from accessing shared data. However, the process of the program is not flexible. How can we make it happen before the current thread has exited the synchronized data block?
Does his thread also have the opportunity to access Shared data? In this case, these three methods are used for flexible control.

The wait () method suspends execution of the current thread and releases the object lock mark so that other threads can enter the synchronized data block. The current thread is put into the object waiting pool. When the notify () method is called, an arbitrary thread will be removed from the object wait pool and placed in the lock sign wait pool, only

The lock sign waits for the thread in the pool to obtain the lock sign. If the lock sign waits for no thread in the pool, then notify () does not work.

Policyall () removes all threads waiting for that object from the object waiting in the pool and puts them in the lock sign waiting in the pool.

Note that all three methods are Java. Lang. ojbect methods!

 

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.