Java Learning Day 22nd (about lock lock in Java and multi-threaded interview questions)

Source: Internet
Author: User
Tags thread class

After JDK5, Java provides a more convenient security mechanism that uses lock locks for thread-locked operations and release operations without using synchronized . More clear. Example:

Lock lock=new Reentrantlock ();

Lock.lock

Code that needs to be locked

Lock.unlock

Why is lock more secure? Because synchronized may cause deadlock phenomenon, specifically about the deadlock phenomenon can refer to the operating system, there is no more to say.

Summarize the main problems in the thread

First,

There are several ways to implement multithreading

There should be three, the first inherits the thread class, and the second is the implementation of the Runnable interface. The third is related to the thread pool.

There are several ways to synchronize

Synchronous code blocks and synchronous methods, where several synchronization methods are divided into static and non-static, non-static lock objects using this can, static use. Class.

Start a thread is run () or start ()?

Using Run is really just a simple method call, regardless of thread, and using Start (), a Java Virtual machine creates a thread that executes the run method with that thread.

The difference between sleep () and wait ()

Sleep must contain the time needed for sleeping and do not need to wake up, while wait is locked and needs to be awakened.

Java Learning Day 22nd (about lock lock in Java and multi-threaded interview questions)

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.