主要讲到了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. }
分享到:
相关推荐
### Java中的多线程学习总结 #### 一、线程与进程的概念 在计算机科学中,**进程**和**线程**是两个重要的概念。早期的Windows 3.x系统中,进程是最小的运行单位。到了Windows 95/NT等操作系统中,除了进程外还...
Java多线程是Java编程中的一个核心概念,它在现代软件开发中扮演着至关重要的角色。...通过深入学习和实践上述Java多线程的知识点,开发者能够构建出高效、稳定、可控的多线程程序,满足各种复杂的并发需求。
在“Java多线程学习总结6”这个主题中,我们可以深入探讨Java多线程的实现、管理及优化。下面将详细阐述相关知识点。 1. **线程的创建方式** - **继承Thread类**:自定义类继承Thread类,并重写run()方法,创建...
下面是对Java多线程学习的详细解析。 1. **多线程概述**: 多线程是指一个程序内可以同时执行多个独立的执行流,每个执行流被称为一个线程。Java通过Thread类来代表线程,每个线程都有自己的生命周期,包括新建、...
Java多线程学习总结.pdf
### Java多线程学习资料知识点解析 #### 一、引言 Java作为一种广泛使用的编程语言,在并发编程领域具有独特的优势。多线程是Java中实现并发处理的核心技术之一,能够显著提升程序的性能和响应性。本文将深入探讨...
### Java多线程编程总结 #### 一、Java线程:概念与原理 - **操作系统中线程和进程的概念** 当前的操作系统通常都是多任务操作系统,多线程是一种实现多任务的方式之一。在操作系统层面,进程指的是内存中运行的...
总结来说,Java多线程学习和FTP上传结合,可以帮助我们构建高效、可控的文件上传服务。通过线程池,我们可以更好地管理并发任务,优化资源使用,提高FTP上传的性能。学习这些内容对于Java开发者尤其重要,尤其是在...
本文档("Java线程学习和总结.htm")可能包含了更多关于线程的实例、源码分析和常见问题解决方案,你可以通过阅读来进一步加深对Java线程的理解。同时,"Java线程学习和总结.files"目录下的文件可能是与文章相关的...
Java多线程是编程中的重要概念,尤其在开发高并发、高性能的应用时不可或缺。本文将深入探讨Java中的线程和进程,以及如何在Java中实现多线程。 首先,理解线程和进程的概念至关重要。线程是操作系统分配CPU时间片...
总结起来,“JAVA多线程编程技术PDF”涵盖了多线程的基本概念、同步机制、线程通信、死锁避免、线程池以及线程安全的集合类等内容。通过深入学习这份资料,开发者可以全面掌握Java多线程编程技术,提升程序的并发...
【Java 多线程学习详细总结】 在Java编程中,多线程是处理并发执行任务的关键技术。本文将深入探讨Java中的多线程概念、实现方式、线程状态转换、线程调度、线程同步以及数据传递等相关知识。 1. **扩展`java.lang...
JAVA多线程与并发学习总结 本文总结了JAVA多线程与并发的相关知识点,涵盖计算机系统的高速缓存机制、JAVA内存模型、内存间交互操作、volatile型变量、原子性、可见性与有序性、先行发生原则等内容。 计算机系统...
### Java多线程学习总结 #### 一、Java多线程基本概念 1. **线程状态** - Java中的线程状态分为以下几种:新生(New)、可运行(Runnable)、运行(Running)、等待/阻塞(Waiting/Blocked)以及终止(Terminated...