Thread Threading Class

Source: Internet
Author: User
Tags garbage collection instance method thread class

Set the thread name

It is easy to see the name of the thread and call it Thread.currentThread().getName() .

PublicClassMythreaddemo {Publicstatic void main new MyThread (); //with a parameter constructs a thread name  thread thread1 =  new Thread (MyThread,   "follow the public" java3y "); Thread thread2 =  new Thread ( MyThread,   "QQ Group: 742919422");  Thread1.start (); Thread2.start (); //print the name of the current thread System.out.println ( Thread.CurrentThread (). GetName () ); }} 
Daemon Threads

A daemon thread is a service to another thread.

    • The garbage collection thread is the daemon thread ~

A daemon thread has a feature :

    • When another user thread finishes executing, the virtual machine exits and the daemon is stopped.
    • That is, the daemon is a service thread and there is no need to continue running without a service object

The place to be aware of when using threads

    1. Set as the daemon thread before a thread is started bysetDaemon(boolean on)
    2. Use a daemon thread to not access shared resources (databases, files, and so on), because it may be hung up at any time.
    3. The new thread that is generated in the daemon thread is also the daemon thread
Priority thread

High thread priority simply means that the thread gets a high chance of CPU time slices , but this is not a definite factor !

The priority of a thread is highly dependent on the operating system , and Windows and Linux differ (Linux priority may be ignored) ~

As you can see,Java provides a default priority of 5, with a minimum of 1, up to ten:

Thread life cycle

Threads have 3 basic states: Execute, Ready, block

Many of the methods on thread are used to toggle the state of a thread,

Sleep Method (A static method in the thread class, Thread.Sleep () is called after the thread that is in the wait)

The call to the sleep method goes into the timed wait state, and when the time is up, it enters the ready state and not the running state ! ( note that the sleep method does not release the lock )

Yield Method (static method in the thread class, after which the current thread is called to give control of the CPU)

Calling the yield method causes the other threads to execute , but does not ensure that the actual

I'm free, if I may, let you do it first .

Join Method (is the final method, thread 1 object. Final (), thread 1 has precedence, it executes before executing other threads)

Calling the Join method waits for the thread to execute until it finishes executing the other thread ~

Interrupt method

The thread break has a stop method in the previous version, but it is set out of date. There is no way to force a thread to terminate now!

Because the Stop method allows one thread A to terminate another thread B

    • Terminated thread B immediately releases the lock, which may leave the object in an inconsistent state .
    • thread A also does not know when thread B can be terminated , and if thread B also handles the run calculation phase, thread A calls the Stop method to terminate thread B, which is very innocent ~

All in all, the Stop method is too violent and unsafe to be set out of date.

We typically use interrupt to request a terminating thread ~

  • Note that INterrupt does not really stop a thread, it simply sends a signal to the thread to tell it that it should be over (it is important to understand this!). )
  • That is to say: the Java designer actually wants the thread to terminate itself , through the above signal , can judge what business to deal with.
  • Exactly whether the interrupt or continue to run, should be handled by the notified thread itself
  •  thread t1 = new Thread (new Runnable () {public void run () {//if no interruption occurs, perform task while (! Thread.currentThread.isInterrupted () ) {//Normal task code ...} //interrupt processing code ... dosomething ();}} ). Start ();             

    Again: Calling interrupt () does not really terminate the current thread , only Only one interrupt flag is set . This interrupt flag can be used to determine when should do the work ! When to break let us decide by ourselves so that you can safely terminate the thread !

  • There are two additional methods for interrupt thread interrupts (check if the thread is interrupted):[Checking for interrupts is one of the uses of interrupt]

    • static method Interrupted () -Clears the interrupt flag bit
    • Instance method isinterrupted () and does not clear the interrupt flag bit
    • "Purpose two: use to block a thread, or to enter a blocked thread, set the interrupt state (that is, using interrupt ()), throw interruptexception, interrupt state Reset, and the thread will exit the blocked "

Thread Threading Class

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.