Javase Basic Note 12

Source: Internet
Author: User
Tags time and date

11th Chapter Multithreading


    1. Understanding the concepts of programs, processes, threads

The program can be understood as static code.

A process can be understood as an executing program.

A thread can be understood as a process step-down, one execution path of the program.


2. How to create a Java program process (focus)

Way one: Inherit from the Thread class

① creating a subclass that inherits from thread

② overrides the Run () method of the thread class, which implements the function that this child thread will complete within the method

③ creating an object of a subclass

④ Call Thread's start (): Starts this thread, invokes the corresponding run () method

⑤ a thread can only perform a single start

Mode two: Implement Runnable interface

① creating a subclass that implements the Runnable interface

② overrides the Run () method of the thread class, which implements the function that this child thread will complete within the method

③ creating an object of a subclass

④ creates a thread object and passes the object as a parameter to the calling thread's start (): Starts this thread, invokes the corresponding run () method


Two ways to compare:

Contact: Class Thread implements Runnable

The way to achieve better 1. Resolves the limitations of single inheritance 2. If multiple threads have shared data, it is recommended to use implementations, while the classes where the shared data resides can be implemented as runnable interfaces

Thread thread Common method: Start () run () CurrentThread () GetName () setName (String name) yied () join () sleep () isAlive () getpriority () SETPR iority (int i);

Wait () notify () Notifyall ()


3. Thread Life cycle

New ready to run block death


4. Synchronization mechanism of processes (emphasis)

Premise: If we create multiple threads and there is shared data, there is a risk of thread security: when one of the threads is working on the shared data, the other threads are involved, and the operation of the shared data becomes problematic

WORKAROUND: When a thread is required to manipulate shared data, other threads have the opportunity to perform shared data only when their completion completes the shared data

Mode one: Synchronizing code blocks

Synchornized (Sync monitor) {

Manipulating code that shares data

}

1. Synchronization monitor: Commonly known as locks, any one class object can act as a lock, want to ensure thread safety, must require all threads to share the same lock

2. Using the Runnable interface to create multi-threaded, synchronize the lock in the code block, you can consider this, if you use the inherited thread class with caution with this

3. Shared data: Variables that multiple threads need to work together. Define which part of the code is to manipulate the shared data.


Mode two: Synchronous method: Declare the method of operation sharing data as synchornized

For example: public synchronized void Show () {//manipulate code to share data}

1. For non-static methods, using synchronization, the default lock is: this. If you use inheritance to implement multithreading, use caution

2. For a static method, if synchronization is used, the default lock is: The current class itself


Summary: Release lock: Wait ();

Do not release Lock: Sleep () yied () suspend () (obsolete, may lead to deadlock)

Deadlock: Different threads occupy each other's required synchronization resources without giving up, waiting for each other to abandon the synchronization resources they need, forming a thread's deadlock


5. The process of communicating the following three methods must be used in a synchronous code block or in a synchronous method

Wait (): When executing to this method in synchronization, this thread "waits" until the other thread executes the method of notify (), wakes it up, wakes up after its wait () code

Notify () Notifyall (): In synchronization, execute to this method, then wake up one or all of the other threads of Wait ()



The 12th Chapter Java Common Class

1.String class: Is a final class that represents an immutable sequence of characters


String classes and basic data types, wrapper classes, conversions to character arrays, byte arrays

① String---> Basic data type, wrapper class: Parsexxx (String str) that invokes the corresponding wrapper class;

② basic data types, wrapper classes---> Strings: Overloaded vauleof () methods that call strings

③ string---> Byte array: GetBytes () of the calling string

④ byte Array---> string: constructor that invokes a string

⑤ String---> Character array: ToCharArray () that invokes the string;

⑥ Character Array---> string: constructor that invokes a string

⑦string--->stringbuffer: Builder Newstringbuffer with StringBuffer (StringBuffer str)

⑧stringbuffer--->string: ToString method using StringBuffer


StringBuffer class: Variable sequence of characters

Stringbuild: Variable sequence of characters, jdk5.0 New, high efficiency, thread insecure

Common methods: Add append () remove delete (int startindex,int endIndex) modify Setcharat (int n,char ch) Query Charat (int index) insert inset (int index, String str) inverse reverse () length ()


2. Time and Date class

①system class: Currenttimemillis (): Returns the Long value of the current time, starting from January 1, 1970 0:0 00 seconds to the current number of milliseconds.

Often used to calculate the time difference.

②date class: Java.util.Date

Date d = new Date ();//Returns the current time of Date:mon 15:17:01 CST 2014

Date D1 = new Date (15231512541241L);//return parameter at which the Long value corresponds

③simpledateformat:java.text.simpledateformat

Formatting: Date----> Text using the Format () method of SimpleDateFormat

Parsing: Text---> Dates using SimpleDateFormat's Parse () method

④calendar: Calendar class

Get instance: Calendar c = calendar.getinstance ();

Get ()/set ()/add ()/date getTime ()

3.Math class Understanding

4.BigInteger BigDecimal Class Understanding



This article from the "Ah Cheng Blog" blog, reproduced please contact the author!

Javase Basic Note 12

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.