Dark Horse programmer--java Basics-Multithreading

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Thread: A program that is executing. The meaning of multithreading exists: at least two threads are running in the program, one is the main thread of the primary function and the other is the garbage collection thread. Thread Creation Method One: inherits the thread class. To overwrite its run method, call the threading Start method. function: 1. Start thread      2. Run the running method. The goal is to store the custom code in the Run method, so that the thread runs the CPU each time only one program executes, but switches between different threads quickly, showing the randomness of multithreading class demo extends thread{public  void Run () {  }} The Run method is used to store the code that the thread will run. Demo Demo=new demo (); Creating an object creates a thread. The Run method and the Start method Run method  is just the object invocation method, and the Start method is not running thread and the run  method in the thread is started

Threads have their own default name: The way to get the name of a thread. Thread.CurrentThread (). GetName () CurrentThread () Gets the current thread object GetName ()   Gets the thread name set the thread name SetName (), or constructs a method to pass the parameter

The second way to create a thread: Implement the Runnable interface. Create Thread Threads t=new thread (new object name ()), step: 1, define class implementation runnable interface. 2, overwrite the Run method in the interface (used to encapsulate the code that the thread is running). 3, the thread object is created through the thread class and 4, the subclass object that implements the Runnable interface is passed as the actual parameter to the constructor in the thread class.      Why should it be passed? Because you want the thread object to explicitly run the object that the Run method belongs to. 5, call the Start method of the Thread object. Open the thread and run the running method in the Runnable interface subclass. Ticket t=new Ticket ();/* Create Ticket object directly, not create thread object. Because the object can only be created through the new thread class, or the subclass of the new thread class. So eventually you want to create a thread. Since there is no subclass of the thread class, you can only use the thread class. The thread t1=new threads (t);//Create threading. What is the difference between how a thread object and T can be implemented and inherited, as long as the T is passed in as the actual argument to the constructor of the thread class? Inheriting the thread class: The thread code block is implemented runnable in the Run method of the thread subclass, and the thread code is stored in the Run method of the sub-class of the interface and can be implemented more. There are limitations to how inheritance works. A class that is to be implemented multithreaded can no longer inherit the thread class if it inherits the parent class.

Multithreading Security: Security causes: When multiple statements operate on the same shared data, one thread executes only a portion of the multiple statements, and the other thread participates in the execution. The error that caused the shared data. WORKAROUND: A statement that shares data on multiple operations can only have one thread complete. During execution, other threads are not allowed to participate in execution. Way: Sync code block: Synchronized (object), train toilet case  {  The code that needs to be synchronized. (Shared Data)} An object is like a lock, a thread holding a lock can execute in synchronization, without a thread holding a lock, even if it gets the execution of the CPU, because no lock is acquired. Prerequisites for synchronization: 1. Must have two or more than two threads 2. Multiple threads must use the same lock.  you must ensure that only one thread in the synchronization is running. Benefits: Solves the problem of thread security: consuming resources, multiple threads need to determine the lock.
Inter-thread communication: Multiple threads operate on the same resource, but the actions are different. 1. is not two or more than two threads. Workaround two threads are to be synchronized. 2. is not the same lock. Workaround find the same object as the lock. Wait for the wake mechanism. After wait, the thread is present in the thread pool, and the threads in the thread pool wake up after notify. Notifyall (); Wakes all threads in the thread pool. Implementation method: Add a tag to the resource flag   synchronized (r) {while (R.flag)//multiple producers and consumers   if (r.flag)//a producer and consumer  r.wait ();  Code   r.flag=true;  R.notify ();  R.notifyall ();} All three of the above methods are used in synchronization, because the thread holding the monitor (lock) is being manipulated. So to use in synchronization, because only synchronization has a lock. Why are the methods of these operations threads defined in the object class? Because these methods are even in the process of synchronizing the threads, they must represent only the locks that they operate on the thread. Only the same locked waiting thread can be awakened by the same locked notify, and the threads in different locks cannot be awakened. That is, the wait and wake must be the same lock, and the lock can be a deliberate object that can be defined in the object class by any method called by any object.


Dark Horse programmer--java Basics-Multithreading

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.