Java multithreaded Programming tutorial thread operation and control

Source: Internet
Author: User

When multiple threads are running concurrently, there are scheduling controls, resource allocations, and so on between multiple threads. This section explains a series of control commands and how to use them in threading

    • Start of Thread (), join () and stop Stop ()

    • Sleep Sleep (), suspend yield for thread

    • Synchronization of Threads synchronized

    • Synchronization lock mechanism for threads: Wait (), notify (), and Notifyall ()


Start () starts the thread and, after the thread is created, starts the thread with the Start method. You can also use the Join method to get threads to execute immediately

Sleep () allows the current thread to pause for a period of time before continuing execution. The function of the sleep () method is to cause the current thread to pause execution for a certain amount of time to give other threads a chance.

The yield () method is similar to the sleep () method, except that it cannot be specified by the user for how long the thread is paused. The sleep () method allows a low-priority thread to be executed, and the yield () method only causes the thread with the same priority to have an opportunity to execute.

The synchronization synchronized of the thread.

Many threads must consider sharing data or coordinating execution with other threads during execution, which requires a synchronization mechanism. Each object in Java has a lock that corresponds to it. Javajava does not provide a separate lock or unlock operation. It is implemented implicitly by the high-level structure to ensure the correspondence of the operation.

Synchronized with methods

Public synchronized void Funname () {//Protection method module}

A better approach is if a variable is assigned a value by a thread and is referenced or assigned by another thread, then all access to the variable must be within a synchronized statement or Synchronized method

Synchronized (this) {}//Protection code module

With the Synchronized keyword, the results of the multi-threaded function will become controllable. The Synchronized keyword is used to protect shared data.


Synchronization mechanism for Threads: Wait (), notify (), Notifyall ()

During the execution of the synchronized code, the thread can call the object's Wait () method, release the object lock flag, enter the wait state, and call the Notify () or the Notifyall () method to notify other threads that are waiting. Notify () notifies the first thread in the wait queue. Notifyall () Notifies all threads waiting on the queue.

The Wait () and notify () mechanisms provided in Java are as follows:

Called by thread 1 synchronized method1 (...)    {//Access data variable available = true; Notify ();} Called by thread 2 synchronized method2 (...) {while (!available) {try{wait ();//wait for notify}catch (Exceptioin e) {}//Access data variable}}















This article is from the "Ah Cool blog source" blog, please make sure to keep this source http://aku28907.blog.51cto.com/5668513/1773894

Java multithreaded Programming tutorial thread operation and control

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.