Java multithreading and thread state transitions

Source: Internet
Author: User
Tags thread class

There are two ways to implement multithreading in Java, one is to inherit the thread class and the other is to implement the Runnable interface.


First, inherit the thread class

public class ThreadTest extends Thread {    @Override public    void Run () {        System.out.println ("This thread name I S: "+thread.currentthread (). GetName ());    }    /**     * @param args */public    static void Main (string[] args) {        threadtest t = new threadtest ();        T.setname ("Mytestthread");        T.start ();    } }

    • After inheriting the thread, you need to overwrite run () to implement your business logic;
    • The start of a thread is called the Start method, not the Run method;
    • When you set the name of the thread, we can get the thread name set.

second, realize runnable interface

public class Runnabletest implements Runnable {    @Override public    void Run () {        System.out.println ("This Thread name is: "+thread.currentthread (). GetName ());    }         public static void Main (string[] args) {        System.out.println (Thread.CurrentThread (). GetName ());        Runnabletest r = new Runnabletest ();        Thread t = new Thread (r);        T.start ();    }}

    • Implementing the Runnable interface requires implementing the Run method and implementing its own business logic within the Run method;
    • The class that implements the Runnable interface cannot start the thread itself, it needs to pass the object of this class to thread, which is started by the thread's Start method.
    • The main function is the entry for Java to start, which is called by a name called main thread, and if a thread does not have a specific set name, the program defaults to setting the name to Thread-num,num, which is a number that starts at 0.

Third, the state transition of the thread


    • When a thread executes the Start method, it does not mean that the thread will be executed immediately, only that the thread is in a running state, and ultimately the thread of the OS is dispatched to determine which running state the thread is to execute.
    • A thread is selected to execute a time limit, this time period is called the CPU time slice, when the time slice runs out but the thread is not finished, the thread will become operational, waiting for the OS to be dispatched again; Execute Thread.yeild () in the running thread Method can also make the current thread operational.
    • When a running thread waits for user input, calls Thread.Sleep (), calls the join () method of another thread, the current thread becomes blocked.
    • The thread of the blocking state is completed by the user, the sleep time, and the join thread ends, and the current thread becomes operational by blocking.
    • The running thread calls the Wait method, and this thread enters the waiting queue.
    • When a running thread encounters synchronized without a lock tag for the object, the thread waiting for the queue waits, the thread waiting for the queue is awakened by the Notify method, and another thread calls the Notifyall method, the thread becomes the lock pool state.
    • The thread of the lock pool state obtains an object lock tag, and the thread becomes operational.
    • The thread runs at the end of the running thread when the Run method finishes executing or the main thread ends.

four, thread synchronization of different methods of the difference

    • Thread.yield () The thread that is currently running becomes a running state.
    • T2.join () causes the current thread to be blocked until the T2 thread finishes executing.
    • Thread.Sleep () causes the current thread to be in a blocking state until the time of sleep ends.
    • The wait, notify, and Notifyall methods are methods of the object class whose calling environment must be called in a synchronized synchronization block, or the java.lang.IllegalMonitorStateException exception will be thrown.


Java multithreading and thread state transitions

Related Article

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.