Java Basics Day 21st-Multithreading 2__java_concurrency

Source: Internet
Author: User
Tags thread class

1. A few minor problems
1 A Thread object, you can call the start () method multiple times.
No, otherwise there will be java.lang.IllegalThreadStateException anomalies.
An exception that indicates that the thread is not in the State requested by the request operation
2 Multiple threads are sharing a run () method, or each thread object executes with its own run () method
Each thread object has its own run () method execution, which encapsulates code that is multithreaded
So, each thread has its own run () method, but multiple threads can work together on the same resource

2. Deadlock problem: Multiple threads using multiple locks, waiting for each other

3. JDK5 new synchronization Operations--there is a lock interface and a Reentrantlock class that implements the lock interface
You can specify when to lock and when to unlock

Class x{
  private final reentrantlock lock = new Reentrantlock ();
  
  public void M () {
    lock.lock ();
    try{}
    finally{
      lock.unlock ()
   }
  }
 }

4. Communication between threads--different kinds of threads
1 How to ensure that multiple objects operate on the same resource.
We created the student object in the test class and then passed the student object as a constructor parameter to a different thread action class
2 If it is a different kind of thread, please ensure that the locks of multiple threads must be the same

5. Wake-up mechanism--wait () + notify () combination
Object class has Wait () and notify () method

The characteristics of the Wait () method:
Where to wait, where to release the lock. When I wake up, I wake up from the last place I waited.

One interview question: The difference between sleep () and wait ().
1 sleep (), must specify the time; Do not release the lock
2 Wait, you can specify the time, or you can not specify the time; release the lock

6. Each thread under the thread class has a priority
Public final void setpriority (int newpriority)
Public final int getpriority ()

Thread default priority is 5 max_priority--10 min_priority--1

The priority of a thread is generally not effective
In the case of a particularly high number of threads, the object with higher thread priority may get precedence first than thread priority

7. Suspend thread
public static void yield ()-pauses the executing thread object and executes other threads
Pausing a thread can, to some extent, allow the threads to run in harmony with some
If you want to ensure data harmony, please use the wait-wake mechanism

8. Public final void Join ()
Other threads must wait for the join thread to finish executing
Note: Adding threads is an action that must be followed by a thread start ()

9. Daemon Thread:
The main thread ends, then all the daemons end immediately.

10. The simplest thread program code

 new Thread () {public void run () {//code ...}
 
 }.start ();
 New Thread (New Runnable () {public void run () {//code ...} }). Start (); 

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.