Java interview in the "rival" thread, 9 questions comprehensive analysis

Source: Internet
Author: User
Tags thread class

I'm a siege master.

(a) How threads are created

(1) Implement Runnable interface

(2) inherit the thread class

It is recommended to use the interface, to be able to define and realize separation, lower coupling

(ii) Priority on threads

Thread2.setpriority (thread.max_priority)

Higher level, priority operation

(iii) method of yield on threads

About the yield method, which means that the current thread may be running a less important task, which implies that the operating system thread can be dispatched by this method I can execute it later, give priority to the CPU resources to me or greater than my task, and if no threads are found that meet the criteria, then the task of that thread will also be executed.

(iv) The Join method on threads

In the main method, start a thread A, and if the A.join () method is executed, the main thread must wait for the a thread to finish executing before the code after the join is executed.

(v) The Sleep method on threads

Let the current thread sleep for a certain period

(vi) Interrupt method for threading

Used to tell a thread should stop, if this thread is currently executing the Wait,sleep,join method will throw interruptedexception, if the normal state, we can through isinterrupted () =true, To stop the thread from using the return method.

(vii) Start and run methods for threads

Note that for a thread to run, only the Start method is executed, this method is the native method, the Run method is where we define the task, and a separate call to the Run method is the same as invoking the normal method. The Start method can only be called once, and more than once will throw an illegal state exception.

(eight) about the state of the thread

In Oracle's official website document, there are six types of threads defined:

(1) New, thread not started after executing new thread ()

(2) RUNNABLE, after the execution of new, the Start method was executed

(3) BLOCKED wait for a lock monitor, or wait to enter a synchronization block or method

(4) Watting waits for other threads to perform a specific operation without any time limit

(5) Timed_waiting wait for other threads to be in a certain time period

(6) TERMINATED the thread after completing the task

Here is a picture, summary of the more comprehensive we can look at:

(ix) Summary

This article mainly introduces some of Java's basic methods and concepts of threading, and finally describes the state of the thread runtime, especially the thread scheduling is dependent on the underlying operating system, running on different systems may get different results, this needs to be noted.

Java interview in the "rival" thread, 9 questions comprehensive analysis

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.