Multithreading (2) - thread of some commonly used methods and keywords

1.start () and run ()

  start () method is a method to open the thread, after the implementation of this method, a thread really enter RUNNABLE state. run () method is thread specific business activities carried out are generally required to override the run () method.

2.stop()

  stop () method at the end of the thread, the thread will terminate directly and immediately release the thread held a lock, and the lock is precisely the object used to maintain consistency. Such as writing thread is half written, was forced to terminate the thread, the object will be written bad, but the lock has been released, other objects read this object is written broken objects.

3.interrupt()、isInterrupted()、interrupted()

  interrupt () method is an instance method, it informs the target thread interrupt, the interrupt flag is set. Is equivalent to thread sends a notification that you want out of the dignitaries. As for the future target thread to see how to deal with the target thread own. isInterrupted () method is an instance method, he determined whether the current thread is interrupted. interrupted () is a static method can be used to determine the current thread's interrupt status, and it will clear the current thread's interrupt flag status.

4.sleep()

  sleep () method is to let the current thread to sleep for some time, it will throw an InterruptedException interrupt exception. This exception must catch and handle the exception is not running, when the threads sleep () sleep, if interrupted, this exception will be generated. Once interrupted, an exception is thrown, it will clear the flag bit, if not addressed, when the next cycle begins, you can not capture this interruption, it is generally then set the flag in the exception handling. sleep () method does not release the lock resource of any object.

5.wait () and notify ()

  wait () wait method is a method in the class Object, when an object instance of the call wait () method of the current thread will wait on the object. So what awaits the end of it? Thread to wait until another thread calls the object's notify () method so far. Examples of such objects became a means of communication between multiple threads. wait () and notify () can not just call, it must be included in the corresponding synchronize statement, maybe the method, we need to first obtain a listener of the target object, and wait () and notify () method will be released after execution this listener. wait () method will release the lock of the target object.

6.suspend()和resume()

  suspend () method hung threads, resume () method continues to be suspended thread must () to continue until the specified resume. suspend () method at the same time cause the thread suspended, and will not release any lock resources, while other threads to access it was occupied by the lock will be implicated in the cause can not function properly, only to encounter resume () operation to continue. Suspended suspend the thread, the thread state from its point of view, actually, or RUNNABLE, will affect the judgment. So maybe method has been abandoned, it is not recommended.

7.join()和yeild()

  join () method indicates an infinite wait, he would have been blocking the current thread, only to the target thread is finished. join (long millis) gives a maximum waiting time, if more than a given time target thread is still running, the current thread will not wait, and continue down the implementation.

  yeild () method is a static method, if implemented, would he let the current thread CPU. Let the CPU do not represent the current thread is not executed, and will be competing for CPU resources. If a thread is unimportant or low priority, you can call this method, the resources to important thread to do it.

8.volatile

  When Allusion volatile declare a variable, is to tell the virtual machine, this variable is likely to be some modification program or thread. In order to ensure that after modification, all threads can see this change, the virtual opportunity to take some means to ensure the visibility of this variable.

9. daemon threads and user threads

  Daemon thread is the guardian of the system, in the background silently systematic services, such as garbage collection threads, JIT-threading. User thread is the system of worker threads to complete the system to complete the operation because, if all the user end of the thread, then the daemon thread would not exist, the whole process will be over. When a Java application, only daemon threads, Java virtual machine denunciation.

  Daemon thread must be set before the start (), Thread.setDaemon (true).

10.synchronize

  Synchronize key role is to achieve synchronization between threads. Its job is to lock the code synchronization, such that each time only one thread can enter a code block, so as to ensure security between threads. Usage: know the lock object, directly on the instance method, the direct effect on the static method.

Guess you like

Origin www.cnblogs.com/wangyongwen/p/11204494.html