http://developer.51cto.com/art/200906/128194.htm
一、线程的基本概念
简单的说:线程就是一个程序里不同的执行路径,在同一个时间点上cpu只会有一个线程在执行,Java里的多线程是通过java.lang.Thread类来实现的,每个线程都拥有自己独立的方法栈空间。
二、java线程的创建和启动
第一种
定义线程类实现Runnable接口:
Thread myThread = new Thread(target) //target为Runnable接口类型
Runnable中只有一个方法:
public void run();用以定义线程运行体
第二种
可以定义一个Thread的子类并重写其run方法:
clas MyThread extends Thread{
public void run(){}
}
线程类必须通过执行Thread的start()方法启动一个新的线程,如果调用run()方法是属于方法的调用,不会启动一个新的线程,推荐使用第一种方式创建线程,使用接口较为灵活。
三、线程状态装换
调用线程start()方法时,线程进入就绪状态,Cpu分配时间片,线程进入运行状态,时间片结束,run()方法未执行完,线程进入阻塞状态。
四、线程控制基本方法 isAlive() //判断线程是否还“活着”,即线程是否还未终止
getPriority() //获得线程的优先级数值
setPriority() //设置线程的优先级指数
Thread.sleep() //静态方法,将当前线程睡眠指定毫秒数
join() //调用某线程的该方法,将当前线程与该线程合并,
//即等待该线程结束,再回复当前线程的运行。
yield() //让出CPU,当前线程进入就绪状态等待调度
interrupt() //中断线程
wait() //当前线程进入对象的wait pool
notify()/all //唤醒对象的wait pool中的一个/所有等待线程
五、sleep方法
Thread的静态方法
public static void sleep(long millis)throws InterruptedException
必须对异常进行捕捉
Thread.currentThread(); //拿到当前线程
http://developer.51cto.com/art/200906/128220.htm
一、interrupt方法一种让线程退出的方式。
1. import
2. public class
3. public static void
4. new
5. t.start();
6. try{Thread.sleep(10000);}
7. catch(InterruptedException i){}
8. t.interrupt();
9. }
10. }
11.
12. class MyThread extends
13. public void
14. while(true){
15. try{
16. "------"+new Date()+"-----");
17. 1000);
18. catch(InterruptedException i){
19. return;
20. }
21. }
22. }
23. }
24.
二、join和yield方法
t.join(); //t的run()方法完才会继续执行当前线程方法体
//也就是两个线程变成了一个线程
t.yield(); //暂停当前正在执行的线程对象,并执行其他线程。方法为静态
//哪个线程体执行此方法,哪个线程让步
1. public class
2. public static void
3. new MyThread3("t1");
4. new MyThread3("t2");
5. t1.start(); t2.start();
6. }
7. }
8. class MyThread3 extends
9. super(s);}
10. public void
11. for(int i =1;i<=100;i++){
12. ": "+i);
13. if(i%10==0){
14. yield();
15. }
16. }
17. }
18. }
三、线程优先级别
线程的优先级用数字表示,范围从1到10,一个线程的缺省优先级为5.
Thread.MAX_PRIORITY=1
Thread.MIN_PRIORITY=10
Thread.NORM_PRIORITY=5
例:t.setPriority(Thread.NORM_PRIORITY+3);
四、线程同步
1.同步代码块
synchronized(this){ //在执行代码块过程中,不会被其他线程打断
...
}
public sunchronized void method //执行此方法时,当前对象被锁定
在Java语言中,引入了对象互斥锁的概念,保证共享数据操作的完整性,每个对象 都对应一个可称为"互斥锁"的标记,这个标记保证在任一时刻,只能有一个线程访 问该对象。
2.线程死锁
1. public class TestDeadLock implements
2. public int flag = 1;
3. static Object o1 = new Object(), o2 = new
4. public void
5. System.out.println("flag="
6. if(flag == 1) {
7. synchronized(o1) {
8. try
9. 500);
10. catch
11. e.printStackTrace();
12. }
13. synchronized(o2) {
14. "1");
15. }
16. }
17. }
18. if(flag == 0) {
19. synchronized(o2) {
20. try
21. 500);
22. catch
23. e.printStackTrace();
24. }
25. synchronized(o1) {
26. "0");
27. }
28. }
29. }
30. }
31.
32. public static void
33. new
34. new
35. 1;
36. 0;
37. new
38. new
39. t1.start();
40. t2.start();
41.
42. }
43. }
五、生产者消费者问题
1. public class
2. public static void
3. new
4. new
5. new
6. new
7. new
8. new
9. new
10. }
11. }
12.
13. class
14. int
15. int
16. this.id = id;
17. }
18. public
19. return "WoTou : "
20. }
21. }
22.
23. class SyncStack { //栈实现
24. int index = 0;
25. new WoTou[6]; //相当于装物品的篮子
26.
27. public synchronized void push(WoTou wt) { //生产物品,线程安全
28. while(index == arrWT.length) { //当篮子满了线程等待
29. try
30. this.wait();
31. catch
32. e.printStackTrace();
33. }
34.
35. }
36. this.notifyAll(); //开始生产时,叫醒等待的其他线程开始消费
37. arrWT[index] = wt;
38. index ++;
39. }
40.
41. public synchronized WoTou pop() { //消费物品,线程安全
42. while(index == 0) { //如果篮子空了
43. try
44. this.wait(); //线程等待,等待生产者开始
45. //生产,叫醒此线程
46. catch
47. e.printStackTrace();
48. }
49.
50. }
51. this.notifyAll(); //消费时喊醒生产者生产
52. index--;
53. return
54. }
55. }
56.
57. class Producer implements Runnable { //生产者类
58. null;
59. Producer(SyncStack ss) {
60. this.ss = ss;
61. }
62.
63. public void
64. for(int i=0; i<20; i++) { //生产20个
65. new
66. ss.push(wt);
67. "生产了:"
68. try
69. int)(Math.random() * 200));
70. catch
71. e.printStackTrace();
72. }
73. }
74. }
75. }
76.
77. class Consumer implements
78. null;
79. Consumer(SyncStack ss) {
80. this.ss = ss;
81. }
82.
83. public void
84. for(int i=0; i<20; i++) { //消费20个
85. WoTou wt = ss.pop();
86. "消费了: "
87. try
88. int)(Math.random() * 1000));
89. catch
90. e.printStackTrace();
91. }
92. }
93. }
94. }