[关闭]
@ZeroGeek 2016-08-25T12:26:30.000000Z 字数 2956 阅读 685

多线程和并发

Java


知识点

线程的状态

  1. // Thread 源码片段
  2. public enum State {
  3. /**
  4. * Thread state for a thread which has not yet started.
  5. */
  6. NEW,
  7. /**
  8. * Thread state for a runnable thread. A thread in the runnable
  9. * state is executing in the Java virtual machine but it may
  10. * be waiting for other resources from the operating system
  11. * such as processor.
  12. */
  13. RUNNABLE,
  14. /**
  15. * Thread state for a thread blocked waiting for a monitor lock.
  16. * A thread in the blocked state is waiting for a monitor lock
  17. * to enter a synchronized block/method or
  18. * reenter a synchronized block/method after calling
  19. * {@link Object#wait() Object.wait}.
  20. */
  21. BLOCKED,
  22. /**
  23. * Thread state for a waiting thread.
  24. * A thread is in the waiting state due to calling one of the
  25. * following methods:
  26. * <ul>
  27. * <li>{@link Object#wait() Object.wait} with no timeout</li>
  28. * <li>{@link #join() Thread.join} with no timeout</li>
  29. * <li>{@link LockSupport#park() LockSupport.park}</li>
  30. * </ul>
  31. *
  32. * <p>A thread in the waiting state is waiting for another thread to
  33. * perform a particular action.
  34. *
  35. * For example, a thread that has called <tt>Object.wait()</tt>
  36. * on an object is waiting for another thread to call
  37. * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
  38. * that object. A thread that has called <tt>Thread.join()</tt>
  39. * is waiting for a specified thread to terminate.
  40. */
  41. WAITING,
  42. /**
  43. * Thread state for a waiting thread with a specified waiting time.
  44. * A thread is in the timed waiting state due to calling one of
  45. * the following methods with a specified positive waiting time:
  46. * <ul>
  47. * <li>{@link #sleep Thread.sleep}</li>
  48. * <li>{@link Object#wait(long) Object.wait} with timeout</li>
  49. * <li>{@link #join(long) Thread.join} with timeout</li>
  50. * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
  51. * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
  52. * </ul>
  53. */
  54. TIMED_WAITING,
  55. /**
  56. * Thread state for a terminated thread.
  57. * The thread has completed execution.
  58. */
  59. TERMINATED;
  60. }

用法

JAVA多线程实现方式主要有以下三种:

1、继承Thread类
2、实现Runnable接口
3、使用ExecutorService、Callable、Future实现有返回结果的多线程

Thread的基本用法

  1. thread.start(); // 启动线程
  2. thread.isAlive(); // 判断是否“存活”
  3. thread.isDaemon(); // 是否守护线程
  4. thread.isInterrupted(); // 是否中断,不设置
  5. thread.interrupt(); // 测试线程是否中断状态,之后设置为false
  6. thread.join(A); // A线程执行完后,再执行thread
  7. thread.setPriority(Thread.MAX_PRIORITY); // 设置优先级 1~10,JVM抢占式调度模型
  8. // 以下方法已经被弃用
  9. thread.stop(); // 停止
  10. thread.suspend(); // 暂停,独占
  11. thread.resume(); // 恢复
  12. // 静态方法
  13. Thread.sleep(1000L);
  14. Thread.yield(); // 放弃当前CPU资源,让给其它任务去占用CPU执行时间。但放弃时机不确定
  15. Thread.currentThread();

停止线程

Java中的同步(为了线程安全)

同步不能继承

synchronized互斥锁

存在的问题

非线程安全示例

如何避免问题

Java内存模型

注意点

参考资料

  1. 《Java多线程编程核心技术》
  2. 《Java并发编程实战》
  3. 《深入理解Java虚拟机》第五部分 高效并发

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

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注