Java Multithreading (0) Java threads

Source: Internet
Author: User

Thread

 Threads are the basic unit of system scheduling, and each time a process is created, there are many threads, also called lightweight processes, that have multiple threads in a process, each with its own counter, heap and local variable properties, and the ability to share memory variables.

 Why to use multithreading

1. More processor Cores

2. Faster response times

3. A better programming model

Priority level

  Thread precedence determines the thread properties that a thread needs to allocate more or less of the processor's resources, prioritize by priority, range from 1-10, threads build setpriority (int) methods to modify precedence, default to 5.

The running state of the thread

 

/tr>
status name description
new initial state, thread is built, But not yet built start ()
runnable Running state, Java threads are ready and running in the operating system, collectively known as" Running "
blocked blocking state, thread blocking and locking
Waiting wait state, thread enters wait state
time_waiting timeout wait status
terminated termination status

In thread priority, JPS can be found in its own life cycle, not in a fixed state, but as code execution switches between different states

1-1 Thread Run State

Daemon Threads

Support thread, mainly in the program background scheduling and support work, when the JVM does not exist non-daemon thread, the JVM will exit, set the daemon thread by Thread.setdaemon (TRUE)

! The main thread is terminated with the Daemoned method after main.

Boot and Stop

Understanding interrupts

  Interrupt () interrupts it, the thread responds by checking itself for interrupts, or it can call the static method thread.interrupted () to reset the current thread endpoint identity bit.

Expired Stop (), RESUME (), SUSPEND ()

 Deadlocks are not recommended, and the suspend () method does not release a resource that is already occupied when it is invoked, but instead occupies a resource into hibernation, and stop () causes the program to work in an indeterminate state because the thread does not guarantee normal release

Volatile

Used to modify a field to tell the program that access to any variable needs to be fetched from shared memory, and that changes must be flushed back to shared memory synchronously, ensuring that all threads are visible to the variable.

Synchronized

can be modified or used as a synchronous block to ensure that multiple threads at the same time, only one thread in the synchronized block of the method, ensuring the visibility and exclusivity of the variable access to the thread.

 ! Any thread accesses an object, first to get the monitor of the object, if the acquisition fails to enter the synchronization queue, the state is blocked, when the predecessor releases the lock, the release operation wakes up the thread that is blocking the synchronization queue and tries to access the

wait/notification mechanism

  The While method checks to see if the variable is expected, but has the following problems

1. It is difficult to ensure timeliness.

2. Difficult to reduce overhead

So Java has a built-in wait-notification mechanism to ensure contradictory resolution (Java. Lang. OBJECT)

  

More than waiting for a period of time for Ms /tr>
wait/notification related methods
notify () Notifies a waiting thread, returning from Wait ()
notifyall () notify all waiting threads /span>
wait () call Method enters the waiting state and only waits for thread notifications or interrupts to return
wait (LONG)
waiit ( Long,int)   time-out control, minimum nanosecond
   

Use Notify () or Notifyall () to be aware

1. Lock the Calling object first

2. After calling the wait () method, the thread state is changed from running to waiting and the current queue is placed in the wait queue

After the 3.NOTIFY () or Notifyall () method call, the wait thread is still not returned from wait (), and the call to NOTIFY () or notifyall () thread is released before the opportunity to return.

The 4.notify () method moves the wait queue to the synchronization queue, Notifyall () moves all threads to the synchronization queue, and the move state shifts from waiting to blocked

The 5.wait () method returns the previous question is the lock that gets the calling object.

Waiting/Notification Classic paradigm

  Wait

1. Get the lock for an object

2. Call wait when the condition is not met ()

3. Condition meets execution

Notice

1. Get the object lock

2. Changing conditions

3. Notifies all waiting object threads

Pipeline I/O flow

Mainly used for data transmission, media for memory

Mainly include: Pipedoutputstream,pipedinputstream,pipedreader,pipedwriter

Steps:

Connect (Pipedreader);

Thread.Join ()

Indicates that the current thread waits for the thread thread to terminate before returning from Thread.Join ().

ThreadLocal

Thread variables, with threadlocal objects as keys, arbitrary objects as worthy storage structures, are attached to threads, and a thread can query the values on this thread based on a threadlocal variable object

Set a value through set (T), get () to obtain

Java Multithreading (0) Java threads

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.