Java多线程-基本概念

参考

http://lavasoft.blog.51cto.com/62575/99150

http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process/

http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html

http://www.programcreek.com/2009/03/thread-status/

http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html

 

概念

进程是应用程序的一个执行实例

线程是进程中的一个执行流程

 

特点

每个进程拥有独立的内存空间

一个进程可以有多个线程

线程轻量级,不需要独立的内存地址空间

一个进程中的多个线程共享内存地址空间

 

Java多线程

在Java中线程由Thread类表示,它实现了Runnable接口;Runnable接口是线程的通用的协议,任何多线程对象都需要实现Runnable接口;Thread类封装了对线程管理的行为

Java中有两种使用线程的策略:

1、直接创建和管理Thread

2、把执行任务交给Executor进行线程管理

 

Java线程的状态

A thread can be in one of the following states:
NEW
A thread that has not yet started is in this state.
RUNNABLE
A thread executing in the Java virtual machine is in this state.
BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.
A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.
WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
  Object.wait with no timeout
  Thread.join with no timeout
  LockSupport.park
A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.
TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
  Thread.sleep
  Object.wait with timeout
  Thread.join with timeout
  LockSupport.parkNanos
  LockSupport.parkUntil
TERMINATED
A thread that has exited is in this state.

 

Java Thread State Transition Diagram

 

posted on 2014-11-05 15:59  ukouryou  阅读(258)  评论(0编辑  收藏  举报

导航