Concurrent programming--java thread-based state transitions

Source: Internet
Author: User
Tags thread class

Basic Concepts of threading

A thread, sometimes called a lightweight process (lightweight PROCESS,LWP), is the smallest unit of program execution flow. A standard thread consists of a thread ID, a current instruction pointer (PC), a collection of registers, and a stack.

--Baidu Encyclopedia

transition state of a thread

Creation of Threads

There are two ways to create a thread, one is the implements self-runnable interface, and the other is to extend from the thread class, both of which need to implement the Run method. When the thread object is new, the thread enters its initial state, and when the thread executes the start method, the thread enters the operational state (very short and quickly goes to the execution state).

runnable Interface
  1  package   Base.newthread;   2   3  public  class  Thread2 extends   Thread {  4   5   @Override   6  public  void   run () {  7   System.out.println (Thread.CurrentThread (). GetName ());   8  }   9  10 } 
Thread Class
1  PackageBase.newthread;2 3  Public classMain {4      Public Static voidMain (string[] args) {5         6Thread1 Runnable1 =NewThread1 ();7Thread T1 =NewThread (Runnable1, "T1");//thread enters initial state8T1.start ();//thread goes into ready state9         TenThread2 t2 =NewThread2 ();//thread enters initial state OneT2.setname ("T2"); AT2.start ();//thread goes into ready state -     } -}
Creation of ThreadsA thread's surrender

When the process is executing, the time slice is exhausted or the yield method is actively executed, the process releases the execution state resource into the operational status waiting to be re-dispatched. The following example generally shows the difference between the call yield and the do not call yield method.

1  PackageBase.yield;2 3  Public classThread1Implementsrunnable{4 5 @Override6      Public voidrun () {7          for(inti = 0; I < 100; i++) {8System.out.println (Thread.CurrentThread (). GetName () + ":" +i);9             //Thread.yield ();Ten         } One     } A}
runnable Interface
1  PackageBase.yield;2 3  Public classThread2extendsThread {4 5 @Override6      Public voidrun () {7          for(inti = 0; I < 100; i++) {8System.out.println (Thread.CurrentThread (). GetName () + ":" +i);9             //Thread.yield ();Ten         } One     } A      -}
Thread Class
1  PackageBase.yield;2 3  Public classMain {4      Public Static voidMain (string[] args) {5         6Thread1 Runnable1 =NewThread1 ();7Thread T1 =NewThread (Runnable1, "T1");//thread enters initial state8T1.start ();//thread goes into ready state9         TenThread2 t2 =NewThread2 ();//thread enters initial state OneT2.setname ("T2"); AT2.start ();//thread goes into ready state -     } -}
Client ClassBlocking of threads

There are three categories of thread entry blocking:

    • Call synchronize into the lock pool state and enter into the runnable state when the thread acquires the lock resource
    • Enter into the lock pool state when the thread acquires the lock resource by calling wait to enter into the wait queue, waiting for another process to call notify or Notifyall, and then enter into the runnable state
    • The thread enters the blocking state by calling sleep, calling join, or blocking waiting for input, and when the sleep time arrives or waits for the process to finish or gets the input results, the threads go into the runnable state.

Concurrent programming--java thread-based state transitions

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.