[Java concurrent programming of the United States] Chapter 1-thread basis (to be updated)

Chapter 1 thread

Threads and processes

  • Process operating system is the basic unit of resource allocation and scheduling, but cpu resources are allocated to the thread, the thread is the basic unit of CPU allocation.
  • Own resources in the thread stack, local variables are stored in the thread private, other threads can not access, in addition to also keep the call stack stack frame thread.

Thread Creation

Three ways: Runnable interface implementing the run method; Thread class inheritance override run and Method; FutureTask manner.

Thread waits for notification

1 wait()

Thread must first obtain prior monitor lock on a shared variable, then when a thread calls wait a shared variable () method, the thread is blocked pending, and freed the lock on a shared variable. Until occurs: (1) Since the other thread calls the shared object notify or notifyAll () method (2) thereafter the other thread calls interrupt the thread () method. (The thread throws InterruptedException Exception Return)

2 wait(long timeout)
  • Timeout parameters timeout: If the thread calling wait (long timeout) hang (after a timeout ms time yet to be awakened) This function returns a timeout because of the waiting time.
  • wait () inside it is called wait (0), both fairly.
3 wait(long timeout,int nanos)
4 notify()

Guess you like

Origin www.cnblogs.com/coding-gaga/p/11291532.html