Java Basics-Day 23rd

Source: Internet
Author: User

Multithreading: Multiple execution units

Process: Running Application

Threads: The execution unit of a process, an execution path

Thunder Download, 360 cleanup

Multithreaded Program implementation:

Threads are dependent on process existence, thread class

Subclass Inheritance

Subclasses overriding the Run method

Call Strart (), let the thread start, call the Run () method

The same thread object started twice in a row, error: Indicates a problem with the thread state

GetName () Method: Flags different threads, gets the thread name

SetName (): Change thread name

Thread Randomness:

Program execution, preemption CPU resources, CPU switching between multiple programs.

The second implementation:

Create a class to implement the Runnable interface

Overriding the Run method

To create an instance of a class

Create a thread object reference by taking the implementation of the class as the thread's construction parameter

Thread.CurrentThread (). GetName ()

Advantage:

Avoids the limitation of single inheritance;

The way to implement the interface is created only once, better to achieve the separation of data and operations.

Ticket code:

A: Sharing of data; two ways;

B: Sell negative, sell the same ticket-solve the problem way

"The randomness and latency of threads"

Find the source of the problem:

A: See if there is any data sharing;

B: Look at the operation of shared data is not a number of statements;

C: See if it's in a multithreaded program

FIX: Lock the code that satisfies the condition together synchronized

Lock object: The lock of the synchronization code block is any object

Synchronization method: Lock on Method---thread safety problem

This object

Static method: Class name. Class class "Bytecode file" type Object

Dead lock

The less code that is synchronized, the better it is, it is recommended to use a synchronous code block

If a method is all synchronized, use the synchronous method

Inter-thread communication:

Student information input and output: multithreading randomness

Input one, output one---the same

Student Information Entry information Acquisition centralized test class

Java Basics-Day 23rd

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.