Some important method thread: Java

A, start ()

Role: thread object start method is called to make thread.
Points:
(1) start () call just after thread immediately in the Runnable state, not necessarily a direct run , it might be waiting cpu resources.

(2) start () call can be seen as the bottom of the run () method. start () is different from the first application from the CPU spatial another thread to execute the current thread run () code, i.e. the current thread and start () is a separate thread running two threads in opposite spaces. And if only call run (), instead of) by calling start (, just within the current thread of execution, rather than another to open up new space to perform. So, to call only thread object's run () is not going to produce a new thread, only start () will create a new thread space.

Two, sleep ()

Role: to make the current thread to sleep (suspended) within the specified time, the cpu fragment let out to the other thread (provided that no resources of its competition), slowing the execution of the current thread.

Points:
(1) SLEEP () is a static method , only the currently running thread to sleep, unable to control thread object.
(2) If the current thread has acquired the lock, SLEEP () will not let the locks.
(3) When the sleep time, the thread returns to Runnable state, not directly run.
(4) sleep (0) time operating system that is triggered manually assign the operation time slice, that is to make the current thread to compete in other threads, a reallocation of resources.

Three, join ()

Role: thread object to call the method to jump the queue, the current thread must wait for it to finish before continuing execution.
Points:
(1) using the join () can test the working status of a thread. Rather than sleep () estimate the approximate time for the cpu.
After (2) join (time) means that the currently executing thread A time to give call join () thread B specified time, A, B two threads re-competition; and join () will be complete A to B to continue carried out. The join (0) is equivalent to the join () a.
(3) join () underlayer is achieved by calling wait ().

Four, yield ()

Role: Pause the currently executing thread object, and perform other threads.
Points:
(1) call to yield () indicates that the thread is not urgent, you can give in to the other thread.
(2) call to yield () only the threads running from state to Runnable ready state, which means that it will still participate in the selection of thread scheduling, that is the operating system may be selected again. This will not improve any efficiency, only reducing the total CPU cycles.

Five, interrupt ()

Role: if the calling thread is blocked, then he interrupt status bit is set to true, and throw InterruptedException exception.
Points:
(1) interrupt () is not really stop off thread, it just interrupt status bit is reset to true, the thread continues to run.

六、interrupted()

Role: test whether the current thread has been interrupted, and clear the thread's interrupt status bit is false.
Points:
(1) interrupted () is a static method, by calling Thread.
(2) the underlying call is isInterrupted ().

七、isInterrupted()

Role: test whether the current thread has been interrupted.
Points:
(1) and interrupted () different is that it does not clear the interrupt status bits.

   There are about three methods of interruption is a somewhat foggy, look at the test code like to understand:

new Thread(){
            @Override
            public void run() {
                {
                    System.out.println("running A");
                    this.interrupt();//中断位被置为true,但是并不会终止运行
                    System.out.println(this.isInterrupted());//已被中断,应返回true
                    System.out.println(Thread.interrupted());//也返回true,但清除了中断位为false
                    System.out.println(this.isInterrupted());//此时应为false
                }
            }
        }.start();

operation result:
Here Insert Picture Description

八、currentThread()

Role: Returns the current thread.
Points:
(1) is a static method, by calling Thread.
(2) Common Thread.currentThread (). GetName () Gets the current thread name.

Nine, isAlive ()

Role: to determine whether the current thread is active.
Points:
(1) the state of the thread is running or ready thread is considered alive.

   Currently these, later we met would add. (Wait, notify and other methods will be described in the lock of the article)

Published 26 original articles · won praise 0 · Views 497

Guess you like

Origin blog.csdn.net/qq_43336822/article/details/104392378