Concurrent Programming Summary--java Thread Basics 1

Source: Internet
Author: User

Recently nothing, by the way, look at Java concurrency programming things, and then summarize the record, if you can see Help, Haha, on the other hand also for later self-review when you can see.

For concurrent programming, prepare to cut in from a few points:

1, Java thread A few knowledge

2, Juc Atomic class

3. Lock

4. Juc Collection

5. Thread pool

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

Split Line start ....

When it comes to multithreading, there are wait () and notify () methods in the hyper-parent class object in Java, including the Runnable interface and the thread class, and of course the thread class implements the Runnable interface.

1. The life cycle of a thread can be divided into five parts:
New state: When a thread object is created, thread thread = new Thread ();

Ready state: Thread is tuned up, waiting for CPU to switch over to run thread Thread.Start ()

Running state: When the CPU switches over and runs the thread

Blocking status: May be wait, join, synchronized, abandon the use of the CPU, suspend operation

Dead state: End of run or exception occurred, thread dead

The above five states can be used on a web-stolen icon shown:

    

2. How do I create a thread?

There are two ways of doing this:

1\ Implement Runnable Interface (implementrunnable indicator implements interface Class)

implementrunnable i = new implementrunnable (); New Runnbale newly created object

New Thread (i). Start (); Create a new Thread object, enter the parameter Runnable implementation class, and invoke the

2\ inherits the thread class (the class that extendthread represents as thread)

Thread thread = new Extendthread (); New Thread Subclass Object

Thread.Start (); Call

Compare the two methods, the first way to implement the interface, the Runnable implementation object as the input parameter, if the new multiple threads call, you can share the object.

And the second, the way to inherit the parent class, new multiple thread run, is independent of each other, and because Java can not be more than the reason for inheritance, the extension does not implement the interface well, it is generally recommended to use the first method.

    

* Note: There is a run () method in the Runnable interface, and when we call a thread we need to call the start () method, and when a thread calls the start () method, the Run method is called when the thread enters the running state. If we call the instance's Run method directly and not a new thread, we simply call the Run method in the current thread.

  

3, about sychronized key words

When there are multiple threads, you can manipulate an object that does not allow multiple threads to operate at the same time, such as having multiple ticket outlets, leaving only one ticket, and not selling the ticket at every ticket, when Java provided the Sychronize keyword to solve the problem. Which ticket to buy the ticket first or according to other rules, you can get the lock, that is, to obtain the sale of this ticket eligibility.

In Java, the synchronization lock is built on the basis of the object, and each object has only one synchronization lock, when called by the sychorized decorated module, obtains the object's synchronization lock, the multi-threaded access synchronization lock mutually exclusive, that is, at the same time, only one thread can get to the object's synchronization lock. For example, thread A and thread B, when thread a acquires an object synchronization lock, thread B calls the sychorized part of the code, does not get the synchronization lock, must wait for a to complete or other reasons to release the synchronization lock, thread B to obtain the object's synchronization lock, and to execute the synchronization content. (if thread B calls the non-synchronized part of the object, it does not need a live lock, it can be called directly)

--The next chapter continues

Concurrent Programming Summary--java Thread Basics 1

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.