Java Threading Beginner (rough)

Source: Internet
Author: User

When we need to do multiple tasks at the same time

We need multiple threads to run different tasks.

This time we need multi-threading

----------------------

Use of multithreading

The first is by inheriting the thread class

Override the Run method in this class

Then create the class object and call the Start method

-------

The second through the implementation of the Runnable interface

Overriding the Run method

Then new out a thread class object and will implement the class object of the Runnable interface

As a parameter.

-------

When we're doing multithreading, sometimes we need to share the same resources.

At this point, we need to use synchronization

is when one of the threads finishes running the synchronization block

To run the next thread to avoid errors

--------

Implementing synchronization

The synchronization code block we used

Synchronized keyword, in the back we need to add a

Lock, this lock is required when an object

Multiple threads require the same lock so that we can execute a thread

Then go on to execute another of course that's not necessarily.

Or we'll use the synchronous method

Java Threading Beginner (rough)

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.