15.java threads (Flaky features)

Source: Internet
Author: User

1. Processes and ThreadsMulti-process: The operating system runs multiple tasks at the same time; Multithreading: a process in process execution;
2. Multi-threaded program operation mode
Single thread and multi-threaded

3. Defining Threading MethodsThe thread is also an object. To Create a threading method:1, define a thread class, inherit Threadand rewrite the method run (), the code inside the run is called the thread body.     (Java only supports single inheritance, and classes defined in this way cannot inherit other classes of Java). Start thread: Object. Start (), called the Run method. 2, the second Method 1) establishes the object of the Runnable class (the task of the thread); 2) generates athread object and passes the object of the Runnable interface implementation class as a parameter to thethe Thread object;Ri isthe Runnable interface implements the object of the class, executing the worker. thread t = new Thread (RI),3) Notify the thread object to execute the start () method;The difference between the two scenarios: Because of Java single inheritance, the second benefit is that thread Cheng and execution threads are split apart. The second type is recommended. 4. Thread-Simple control method1) Middle thread break-thread.sleep ()//Hibernate-thread.yield ()//yield CPU, re-Rob2) Set the priority of the thread (max 10, min 1, static constant to set Max_priority)-getpriority ()//Get priority-setpriority ()//Set Prioritythe higher the probability of a high priority execution, the greater is not the first execution. 4. Multi-threaded data security (access integrity)This error occurs when multiple threads are using the same data. Concurrency issues that cause competitive status.
Scenario: Two thread objects are generated, but these two thread objects share the same thread body;each thread has a name, and the thread name can be set through the SetName () method of the Thread object. You can also usethe GetName method gets the name of the thread, starting two threads, respectively. Synchronous Thread Resolution: synchronizing code blocks (synchronous locks)synchronized (this) {} 5.synchronized 1) Synchronous code block function:once a thread obtains a synchronization lock on an object, all other synchronized code on the object cannot bethe thread executes, and all waits for the synchronization lock to be released after execution. Synchronized (object)
2) Synchronization method:Public synchronized void Fun1 ()
The locked object is also this (the object that called the function) 3) The synchronization method and the synchronization code block differ: The synchronization code block specifies which object is specific. The synchronization method locks the object that called the function.        








From for notes (Wiz)

15.java threads (Flaky features)

Related Article

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.