Java review of multi-threading

Source: Internet
Author: User

A thread is a different path in a program

Example 1

Only one path

Each branch is a thread

In fact, a computer can run only one process at a time, but because the CPU is running fast, we feel like we're working at the same time.

Two ways to create a thread

Example 1:

Example 2:

Inherit from thread

Use interfaces as much as possible

Thread State Switching

Basic methods of threading control

Sleep/join/yield method

Test 1

Test 2

Merge to a Main method

Priority of threads (not all)

Example

Example

How to stop right

Currentthred Get the current thread

Thread synchronization:

Problem description

There's 3000 bucks, you and your wife are going to get the money. You go to the counter to fetch your wife to the ATM machine to pick up the money to check whether it is enough money and finally found enough for you and your wife to take 2000 dollars account changes to 1000, and then you took 4000? This is not possible.

Example

Lock the current object

Example

Interview questions

Operation result is 1000

The lock is just locked in the method.

Producer Consumer Issues

Make steamed bread and eat steamed bread

Notify Wake up Wait

The difference between wait and sleep

Wait is the method of the object, and sleep is the thread.

Wait

Something else is accessible.

Sleep is inaccessible

  

Java review of multi-threading

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.