Multithreading & Timers timer& Sync & Thread Communication &threadlocal

Source: Internet
Author: User
Tags deprecated thread class

1. Multithreading
    • Thread state is divided into: new state, ready state, running state, blocking state, death state
      • Object waits for blocking state of pool: Run state executes the wait method
      • Blocking state to the lock pool: attempting to acquire a synchronization lock that is already occupied by another thread, is placed in the lock pool of the object
      • Other blocking states: the Sleep () method is executed, the Join method ()
    • Thread Sleep Thread.Sleep () method: The current thread abandons the CPU and goes to the blocking state
    • Thread Concession Thead.yield () static method: If another thread with the same priority at this time is in the ready state, then the yield () method puts the currently running thread into the running pool and causes the other thread to run. If there are no running threads of the same priority, this method does nothing.
    • Wait for other threads to end the Join () method: The currently running thread can invoke the join () method of another thread, and the currently running thread will go to the blocking state until another thread finishes running before it resumes running.
    • Machin.join () is the execution of this thread that stops letting the Machin thread run
    • Set the background thread Thread.setaemon (True) method to set the current thread as a background thread
2. Timers Timer
New Timer (true// sets the thread associated with the timer to the background thread new timertask () {// Anonymous inner class implementation of the Run method     //taskused to set the tasks to be performed by the timer; 10 the time to delay execution ms;500 to repeat the task every 500ms }
2. Synchronization
    • Synchronizing code blocks
      • Synchronized (this) {} This represents a lock that references the current class object
    • Synchronous Method Lock
      • Public synchronized String Pop () {}
    • What happens when a lock is released?
      • When the synchronization code block is executed, the lock is released
      • During the execution of a synchronous code block, the thread terminates with an exception, and the lock is freed
      • During the execution of the synchronization code block, the Wait () method of the object to which the lock belongs is executed, which releases the lock and enters the object's waiting pool
    • What happens when the lock is not released?
      • During the execution of the synchronization code block, the Thread.Sleep () method is executed, the current thread discards the CPU, starts to sleep, and does not release the lock during sleep
      • During the execution of the synchronization code block, the Thread.yield () method is executed and the current thread discards the CPU but does not release the lock
      • During the execution of a synchronous code block, other threads execute the supend () of the current thread object, the current thread is paused, but the lock is not released. The Supend () method of the thread class has been deprecated
3. Thread Communication
    • Wait (): the thread that executes the method frees the object's lock, and the Java Virtual machine places the thread in the object's waiting pool. The thread waits for another thread to wake it up
    • Notify (): The thread that executes the method wakes a thread waiting in the object's wait pool. Java Virtual machine randomly selects a thread from the object's waiting pool and transfers it to the object's lock pool
4. Interrupt Blocking
    • When thread A is in a blocking state, if thread B calls thread A's interrupt () method, thread A receives a interruptedexception and exits the blocking state to begin exception handling
5. Line Program Control system
    • Start (): Start thread
    • Suspend (): Suspend thread (deprecated)
    • Resume (): Causes the suspended thread to resume running (discarded)
    • Stop ():: Terminating thread (deprecated)
6. Thread Groups
  • Activecount () method of the Threadgroup class: Gets the number of threads currently or
  • The Enumerate (machines) method of the Threadgroup class: This method stores the currently alive thread reference in the parameter machines
  • Main () {Threadgroup Group=NewThreadgroup ("Machines");  for(inti = 1;i <= 5;i++) { machine machine=NewMachine (Group, "machine" +i);    Machine.start (); }    intActivecount =Group.activecount (); Thread[] Machines=NewThread[activecount];    Group.enumerate (machines);  for(inti = 0;i < activecount;i++) Syso (Machines[i].getname ()+ "Is Alive");}
7.ThreadLocal
    • The Threadlocal class can be used to hold local variables for a thread, each with a separate local variable that is not shared with each other
      • Public T Get (): Returns local variables for the current thread
      • Protected T InitialValue (): Returns the initial value of the local variable of the current thread
      • public void Set (T value): Sets local variables for the current thread
      • There is a map cache in the Threadlocal class where the user stores local variables for each thread

Multithreading & Timers timer& Sync & Thread Communication &threadlocal

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.