Java -- thread creation and common methods

Source: Internet
Author: User

Inherit from java. lang. Thread class -- Thread class represents Thread class

The common method is static Thread currentThread (): return the reference of the currently running Thread object. Static void yield (): Pause the currently running thread object and run other threads. Static void sleep (long millis) Throws InterruptedException: Sleep (paused) the currently running thread within a specified millisecond. This operation is affected by the accuracy and accuracy of the system timer and scheduler. This thread does not release the object lock. Millis indicates the sleep time in milliseconds. Void start (): enables the thread to start running. the Java Virtual Machine calls the run () method of the thread, and the thread runs concurrently with other threads. Void run (): If the thread is constructed using an object that implements the Runnable interface, the run () method of the Runnable object is called; otherwise, the method does not perform any operation and returns the result. The subclass of Thread should override this method.Void interrupt (): sets the thread interrupt flag and the request thread stops running. Final void setPriority (int newPriority): modifies the priority of a thread. Final int getPriority (): the priority of the returned thread. Final void setName (String name): Change the thread name to be the same as the parameter name. Final String getName (): returns the name of the thread. Final void join () throws InterruptedException: Wait for the thread to terminate. Final void setDaemon (boolean on): Mark this thread as a daemon or user thread. When all running threads are daemon threads, the Java Virtual Machine exits. This method must be called before the thread is started. Long getId (): returns the identifier of the thread. The thread ID is a positive long integer that is generated when the thread is created. The thread ID is unique and remains unchanged until the thread ends. After the thread is terminated, the thread ID can be reused. The user's Thread class only needs to inherit the Thread class and override the run () method of the Thread class. You can start the thread by calling the start () method of the user Thread class. Another way to create a thread is to implement the java. lang. Runnable interface, which has only one run () method. When Thread (Runnable thread) is used to create a Thread object, an object implementing the Runnable interface needs to be passed for this method, the created thread will call the run () method of the object implementing the Runnable interface.

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.