@ZeroGeek
2016-08-25T12:26:30.000000Z
字数 2956
阅读 806
Java
// Thread 源码片段
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* 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
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* 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:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
1、继承Thread类
2、实现Runnable接口
3、使用ExecutorService、Callable、Future实现有返回结果的多线程
thread.start(); // 启动线程
thread.isAlive(); // 判断是否“存活”
thread.isDaemon(); // 是否守护线程
thread.isInterrupted(); // 是否中断,不设置
thread.interrupt(); // 测试线程是否中断状态,之后设置为false
thread.join(A); // A线程执行完后,再执行thread
thread.setPriority(Thread.MAX_PRIORITY); // 设置优先级 1~10,JVM抢占式调度模型
// 以下方法已经被弃用
thread.stop(); // 停止
thread.suspend(); // 暂停,独占
thread.resume(); // 恢复
// 静态方法
Thread.sleep(1000L);
Thread.yield(); // 放弃当前CPU资源,让给其它任务去占用CPU执行时间。但放弃时机不确定
Thread.currentThread();
同步不能继承
https://www.ibm.com/developerworks/cn/java/j-concurrent/
http://wiki.jikexueyuan.com/project/java-concurrency/concurrency-multithreading.html
http://ifeve.com/java-concurrency-thread-directory/
https://www.gitbook.com/book/gechentuo/efficient-android-threading/details
http://mrpeak.cn/blog/android-threading/
https://developer.android.com/training/multiple-threads/index.html
http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1022