java-Multithreading (finishing)

Source: Internet
Author: User



Priority >1~10


Thread scheduling
OS:
Preemptive > Higher priority threads Replace the currently running thread
Non-preemptive > does not replace the currently running thread even if there is a higher-priority thread
Jvm:
Green Threading: The simplest threading model. There is only one process for the OS one thread, so
The JVM must maintain the original information of the created thread, etc...


Daemon/non-daemon thread:
The daemon thread ends automatically when the parent thread of the creation thread dies, not the daemon thread
It will not.


Creating Threads
Implement Runnable interface thread class Threadgroup class


Static methods
int Thread.activecount (); Returns the number of active threads in the same thread group as the current thread
Thread Thread.CurrentThread (); return current thread
void Thread.Sleep (long);


Basic operations
1. Setting the Daemon/non-daemon property
Setdaemon (); Set whether to be a daemon thread
2. Start
Call the Start () method and the system callback the Run () method
3. Stop thread/suspend/Resume Thread
Stop ()/suspend ()/resume ();
4. Transfer of control to other threads
Yield () generally refers to getting CPU time from other threads with the same priority.
5. Change the priority level
SetPriority ()
6. Wait for other objects
Obj.wait ()
7. Middle Thread Break
Interrupt () > Message of thread Break in send
Isinterrupted () > Do not empty flag, this method is an object method; Returns whether the current thread is interrupted
thread.interrupted () > returns the flag and clears it, indicating whether the current thread is interrupted


Synchronizing Object Locks
The thread allocation CPU is asynchronous, and the execution order of the threads is also not budget, when different threads access the common
Resources, the resource is not secure, and in the bottom, each thread accesses the resource only
Cache values, different execution sequences result in inconsistent cache resources at different times. Java provides
Synchronized keyword, when a thread accesses the resource for the first time, locks the resource until the thread has finished accessing it.
Other threads are accessible, and other threads cannot access the resource when the thread is interrupted in the access resource.


Object.wait ();/object.notify ();
All 2 of these functions must be used within the synchronized code block or function block.
When the wait method of the object is called, the thread pauses until the notify of the object is called, after the wait method
The code is executed. When the object has multiple wait methods called, the method that wakes the thread wakes the oldest call
The thread for wait. The system also provides a notifyall () method to wake up all waiting objects.

java-Multithreading (finishing)

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.