Java多线程学习总结二
主要讲到了interrupt方法一种让线程退出的方式、join和yield方法、线程优先级别 、线程优先级别 、线程同步、生产者消费者问题五个方面的内容。
一、interrupt方法一种让线程退出的方式。
1.import java.util.*;
2.public class TestInterrupt{
3. public static void main(String[] args){
4. MyThread t = new MyThread();
5. t.start();
6. try{Thread.sleep(10000);}
7. catch(InterruptedException i){}
8. t.interrupt();
9. }
10.}
11.
12.class MyThread extends Thread{
13. public void run(){
14. while(true){
15. try{
16. System.out.println("------"+new Date()+"-----");
17. Thread.sleep(1000);
18. }catch(InterruptedException i){
19. return;
20. }
21. }
22. }
23.}
二、join和yield方法
t.join(); //t的run()方法完才会继续执行当前线程方法体
//也就是两个线程变成了一个线程
t.yield(); //暂停当前正在执行的线程对象,并执行其他线程。方法为静态
//哪个线程体执行此方法,哪个线程让步
1.public class TestYield {
2. public static void main(String[] args) {
3. MyThread3 t1 = new MyThread3("t1");
4. MyThread3 t2 = new MyThread3("t2");
5. t1.start(); t2.start();
6. }
7.}
8.class MyThread3 extends Thread {
9. MyThread3(String s){super(s);}
10. public void run(){
11. for(int i =1;i<=100;i++){
12. System.out.println(getName()+": "+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 Runnable {
2. public int flag = 1;
3. static Object o1 = new Object(), o2 = new Object();
4. public void run() {
5.System.out.println("flag=" + flag);
6. if(flag == 1) {
7. synchronized(o1) {
8. try {
9. Thread.sleep(500);
10. } catch (Exception e) {
11. e.printStackTrace();
12. }
13. synchronized(o2) {
14. System.out.println("1");
15. }
16. }
17. }
18. if(flag == 0) {
19. synchronized(o2) {
20. try {
21. Thread.sleep(500);
22. } catch (Exception e) {
23. e.printStackTrace();
24. }
25. synchronized(o1) {
26. System.out.println("0");
27. }
28. }
29. }
30. }
31.
32. public static void main(String[] args) {
33. TestDeadLock td1 = new TestDeadLock();
34. TestDeadLock td2 = new TestDeadLock();
35. td1.flag = 1;
36. td2.flag = 0;
37. Thread t1 = new Thread(td1);
38. Thread t2 = new Thread(td2);
39. t1.start();
40. t2.start();
41.
42. }
43.}
五、生产者消费者问题
44.public class ProducerConsumer {
45. public static void main(String[] args) {
46. SyncStack ss = new SyncStack();
47. Producer p = new Producer(ss);
48. Consumer c = new Consumer(ss);
49. new Thread(p).start();
50. new Thread(p).start();
51. new Thread(p).start();
52. new Thread(c).start();
53. }
54.}
55.
56.class WoTou {
57. int id;
58. WoTou(int id) {
59. this.id = id;
60. }
61. public String toString() {
62. return "WoTou : " + id;
63. }
64.}
65.
66.class SyncStack { //栈实现
67. int index = 0;
68. WoTou[] arrWT = new WoTou[6]; //相当于装物品的篮子
69.
70. public synchronized void push(WoTou wt) { //生产物品,线程安全
71. while(index == arrWT.length) { //当篮子满了线程等待
72. try {
73. this.wait();
74. } catch (InterruptedException e) {
75. e.printStackTrace();
76. }
77.
78. }
79. this.notifyAll(); //开始生产时,叫醒等待的其他线程开始消费
80. arrWT[index] = wt;
81. index ++;
82. }
83.
84. public synchronized WoTou pop() { //消费物品,线程安全
85. while(index == 0) { //如果篮子空了
86. try {
87. this.wait(); //线程等待,等待生产者开始
88.//生产,叫醒此线程
89. } catch (InterruptedException e) {
90. e.printStackTrace();
91. }
92.
93. }
94. this.notifyAll(); //消费时喊醒生产者生产
95. index--;
96. return arrWT[index];
97. }
98.}
99.
100.class Producer implements Runnable { //生产者类
101. SyncStack ss = null;
102. Producer(SyncStack ss) {
103. this.ss = ss;
104. }
105.
106. public void run() {
107. for(int i=0; i<20; i++) { //生产20个
108. WoTou wt = new WoTou(i);
109. ss.push(wt);
110. System.out.println("生产了:" + wt);
111. try {
112. Thread.sleep((int)(Math.random() * 200));
113. } catch (InterruptedException e) {
114. e.printStackTrace();
115. }
116. }
117. }
118.}
119.
120.class Consumer implements Runnable {
121. SyncStack ss = null;
122. Consumer(SyncStack ss) {
123. this.ss = ss;
124. }
125.
126. public void run() {
127. for(int i=0; i<20; i++) { //消费20个
128. WoTou wt = ss.pop();
129. System.out.println("消费了: " + wt);
130. try {
131. Thread.sleep((int)(Math.random() * 1000));
132. } catch (InterruptedException e) {
133. e.printStackTrace();
134. }
135. }
136. }
137.}