(Primary programmer) thread face questions

Transfer: https://blog.csdn.net/u013129944/article/details/73741161
https://baijiahao.baidu.com/s?id=1610396903519844310&wfr=spider&for=pc
to throw a few questions:
What are the processes and threads ?
There are many computer programs run independently of each program is a separate process, such as QQ is a process, that thread does, QQ download lots of stuff can be downloaded at the same time, this is the thread, it is the smallest unit of execution process!

  1. New (NEW): create a new thread object.

  2. Run (RUNNABLE): After the thread object is created, other threads (such as main thread) calls the start () method of the object. The thread state is located runnable threads in the pool, waiting to be selected thread scheduling, acquiring the right to use the cpu.

  3. Run (RUNNING): runnable (Runnable) obtained threads of cpu time slice (timeslice), execution of program code.

  4. Blocked (BLOCKED): blocking state is the thread for some reason to give up the right to use the cpu, that is let out of the cpu timeslice, temporarily stop running. Until the thread becomes runnable (runnable) state, have a chance to get cpu timeslice to run (running) state again. Where blocking three categories:
    (a) waiting for blocking: a thread of execution o.wait running (running) () method, JVM will thread into the wait queue (waitting queue) in.
    (B) synchronous blocking: thread running (running) when acquiring synchronization lock object, if the synchronization lock is occupied by another thread, the JVM will lock into the thread pool (lock pool) in.
    (3) Other blocking: running (running) thread execution Thread.sleep (long ms) or t.join () method, or issue the I / O request, the JVM will set the thread is blocked. When sleep () timeout, the Join () wait for a thread to terminate or times out, or when the I / O processing is complete, the thread may be re-run into (Runnable) state.

  5. Death (DEAD): Thread run (), main () method has finished executing, or due to abnormal withdrew from the run () method, the thread end of the life cycle. Thread of death can not be resurrected again.
    Thread two more classic picture:
    Here Insert Picture Description
    Here Insert Picture Description
    achieve both methods of threads:
    implement Runnable and inheritance Thread class may get a thread, new one instance out of thread enters the initial state
    comparison of several methods:
    Thread.sleep (Long of millis ), the current thread must call this method, the current thread into the blocked, but does not release the object lock, automatic wake up after millis thread becomes runnable. Role: The best way to give the opportunity to perform other threads.
    Thread.yield (), the current thread must call this method, the current thread to give up cpu time slice acquired by the operating state of the change will be running, let the OS select the threads again. Role: let the same priority thread execution by turns, but does not guarantee that will take turns to perform. We can not guarantee the actual yield () to achieve the purpose of concessions, because concessions threads may also be selected again thread scheduler. Thread.yield () does not cause obstruction.
    t.join () / t.join (long millis ), thread the current thread calls join other method of blocking the current thread, but does not release the object lock until the thread 1 is finished or millis time that the current thread becomes runnable status.
    obj.wait (), the current thread calls the object's wait () method, the current thread releases the object lock, into the waiting queue. Rely notify () / notifyAll () Wake or wait (long timeout) timeout time to automatically wake.
    obj.notify () wake up a single thread waiting on this object's monitor, the choice is arbitrary. notifyAll () wakes up all threads waiting on this object's monitor.
    Continue to update the problem of producers and consumers in the evening:
    ~~

Published 32 original articles · won praise 21 · views 8480

Guess you like

Origin blog.csdn.net/weixin_39638459/article/details/86630081