"Go" Java Learning---communication between threads

Source: Internet
Author: User
Tags mutex

"Original" https://www.toutiao.com/i6572378564534993415/

communication between two threads

This is our previous thread.

Execution effect: Who grabs the resource, who runs it ~

To implement thread execution alternately:

There are two main things to use here.

1) Create a flag flag to allow the program to judge:

When flag! = 1 o'clock, Print1 enters the wait, executes Print2, then lets flag=1, wakes the thread that is waiting for the

When flag! = 0 o'clock, Print2 enters the wait, executes Print1, lets flag=0 wake up waiting threads.

2) There are wait () and notify () methods in the object class that can wait and wake the thread

communication between multiple threads

Two threads are done, so a little bit more?

This time with the Notify () method is not enough, this method can only wake up a single thread, to use Notifyall, wake up all the waiting threads, and then let them match, who meet the conditions who will run ~

Attention:

In the synchronization code block, with which object lock, the Wait () method is invoked with that object.

differences between the sleep () and Wait () methods

The functions of these two methods are very similar, but there is a little difference.

1th:

The sleep () method must give a parameter, the parameter is the time to wait, the time to wake up automatically.

The wait () method can also pass in a parameter, and the program enters the wait after the time at which the parameter represents, without passing the parameter indicating that the program goes directly to wait.

2nd:

The sleep () method does not release a lock in a synchronous code block or in a synchronization function.

The wait () method releases the lock in a synchronous code block or in a synchronization function.

Characteristics of JDK1.5

Mutex lock Reentrantlock

A reentrant mutex lock, which has the same basic behavior and semantics as the implicit monitor lock accessed using the Synchronized method and the statement, but is more powerful.

There is a newcondition () method in this

You can return a Condition instance. This example can call the following method, we mainly use the circle of the two methods ~

Then we implement the above function code, we can write

the life cycle of a thread

is the life cycle of a thread, the small partners to look at, learned a thread, this thing still need to understand the ~

Photo Network

Thread Group

Thread Group Overview

Java uses Threadgroup to represent thread groups, which can classify a batch of threads, and Java allows programs to control thread groups directly.

By default, all threads belong to the main thread group.

Getthreadgroup ()

Get the group that he belongs to through the thread object

GetName ()

Get the name of his group through the thread Group object

We can also set a group for threads

The steps are as follows:

1) threadgroup (String name) creates a thread group object and assigns it a value name

2) Create thread object

3) Thread (Threadgroup, Runnable,name)

4) Set the priority of the whole group or the daemon thread

Code Demo:

thread Pool

program startup A new thread cost is relatively high because it involves interacting with the operating system. Using a thread pool can improve performance, especially when you want to create a lot of short-lived threads in your program.

line constructor Each thread code ends and does not die, but returns to the thread pool again to become idle, waiting for the next object to be used . (Starting with JDK5, Java built-in support thread pool)

Overview of the use of the built-in thread pool

JDK5 added a executors factory class to generate the thread pool, there are several ways

newfixedthreadpool (int nthreads)

Newsinglethreadexecutor ()

The return value of these methods is the Executorservice object, which represents a thread pool that can execute Runnable objects or the threads represented by callable objects. It provides the following methods

Submit (Runnable Task)

Submit (callable<t> Task)

steps to use:

1) Creating a thread pool object

2) Create a runnable instance

3) Submit Runnable instance

4) Close the thread pool

Code Demo:

"Go" Java Learning---communication between threads

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.