一:线程概念
正如计算机操作系统都有进程的概念:在系统中相互隔离、独立运行的程序(而实际上CPU在某个时间点上只能执行一个程序,只是系统在快速连续的切换程序的运行)。
在程序的运行中,我们一样能执行多个任务,每个任务称为一个线程,能够同时运行多个线程的程序称为多线程程序。每一个Java应用程序都至少有一个线程:主线程。当Java应用程序启动时,虚拟机就会创建主线程,并在线程中调用
main()方法。
JVM除了主线程通常还会创建一些不可见的线程,如自动垃圾收集器线程、对象终止或其他的相关线程。
线程与进程的区别:
每个进程都有自己的一组完整的变量,而线程是共享相同的数据。
二:创建一个线程(一)
创建一个类继承Thread,然后覆盖thread中的run()方法,这样就让类本身也成了线程。
Thread包含了一个特殊的方法,叫做start(),他的作用是对线程进行特殊的初始化,然后调用run()方法。所以整个步骤包括:调用构造来创建对象,然后用start()配置线程,在调用run()方法。
//示例1:
Java代码
1. package example;
2.
3. public class TestThread extends Thread{
4. private static int threadCount = 0;
5. private int i = 5;
6. private int threadnumber;
7.
8. private TestThread(){
9. threadnumber = ++ threadCount;
10. System.out.println("创建线程:"+threadnumber);
11. }
12.
13. public void run(){
14. while(true){
15. System.out.println("运行线程"+threadnumber+"("+i+")");
16. if(--i==0)
17. return;
18. }
19. }
20.
21. public static void main(String[] args) {
22. for(int j=0;j<5;j++){
23. new TestThread().start();
24. System.out.println("所有进程开启完毕");
25. }
26. }
27. }
package example;
public class TestThread extends Thread{
private static int threadCount = 0;
private int i = 5;
private int threadnumber;
private TestThread(){
threadnumber = ++ threadCount;
System.out.println("创建线程:"+threadnumber);
}
public void run(){
while(true){
System.out.println("运行线程"+threadnumber+"("+i+")");
if(--i==0)
return;
}
}
public static void main(String[] args) {
for(int j=0;j<5;j++){
new TestThread().start();
System.out.println("所有进程开启完毕");
}
}
}
三:创建线程(二)
我们还可以通过实现Runnable接口并实现接口中定义的唯一的方法run()来创建一个新的线程。
在将实现Runnable接口的类在程序中创建一个实例,并将这个实例做为Thread构造函数的参数创建一个新的实例,调用这个实例的start()方法,它将会调用run()来实现多线程。
//示例2:
Java代码
1. package example;
2.
3. public class TestThread2 implements Runnable{
4. private static int threadCount = 0;
5. private int i = 5;
6. private int threadnumber;
7.
8. private TestThread2(){
9. threadnumber = ++ threadCount;
10. System.out.println("创建线程:"+threadnumber);
11. }
12.
13. public void run(){
14. while(true){
15. System.out.println("运行线程"+threadnumber+"("+i+")");
16. if(--i==0)
17. return;
18. }
19. }
20.
21. public static void main(String[] args) {
22. for(int j=0;j<5;j++){
23. new Thread(new TestThread2()).start();
24. System.out.println("所有进程开启完毕");
25. }
26. }
27. }
package example;
public class TestThread2 implements Runnable{
private static int threadCount = 0;
private int i = 5;
private int threadnumber;
private TestThread2(){
threadnumber = ++ threadCount;
System.out.println("创建线程:"+threadnumber);
}
public void run(){
while(true){
System.out.println("运行线程"+threadnumber+"("+i+")");
if(--i==0)
return;
}
}
public static void main(String[] args) {
for(int j=0;j<5;j++){
new Thread(new TestThread2()).start();
System.out.println("所有进程开启完毕");
}
}
}
与上一个示例相比,有什么区别。
四:线程的状态
线程的状态可分为五种:
新建(new)
可运行(Runnable)
运行(Running)
阻塞(Blocked)
死亡(Dead)
五:线程控制
1.测试线程:isAlive()
2.中断线程:Thread.sleep(),Thread.yield()
3.设置优先级:getPriority(),setPriority()
六:线程的join()方法
join()方法是一个等待另一个线程完成的方法
当调用Thread.join(),调用线程将阻塞,直到被join()方法加入的线程完成为止.
七:线程控制的方法
start() : 新建的线程进入Runnable状态
run() : 线程进入Running 状态
wait() :线程进入等待状态,等待被notify,这是一个对象方法,而不是线程方法
notify()/notifyAll() :唤醒其他的线程,这是一个对象方法,而不是线程方法
yield() :线程放弃执行,使其他优先级不低于此线程的线程有机会运行
getPriority()/setPriority() :获得/设置线程优先级
suspend() :挂起该线程,Deprecated,不推荐使用
resume() :唤醒该线程,与suspend相对,Deprecated,不推荐使用
sleep(): 线程睡眠指定的一段时间
join() :调用这个方法的主线程,会等待加入的子线程完成
八:多线程的作用
JAVA中的多线程是通过虚拟CPU来实现的,通过java.lang.Thread类来实现虚拟其内部的功能,能接收和处理传递给它的代码和数据,并提供了独立运行的控制功能。
多线程的应用很广泛,如一个浏览器同时下载多幅图片,一个服务器能够响应并发的请求,还有JAVA程序后台的垃圾收集器都是多线程的应用。
九:Java线程模型
虚拟的CPU,由java.lang.Thread类封装和虚拟CPU来实现。
CPU所执行的代码,传递给Thread类对象。
CPU所处理的数据,传递给Thread类对象。
十:同步的安全性
嵌套同步是安全的。
同步化方法:
synchronized可以写在放法前,表示锁定this
void 方法名(){
synchronized(this){}
}
==
synchronized void 方法名(){}
十一:多线程编程一般规则
如果两个或两个以上的线程都修改一个对象,那么把执行修改的方法定义为被同步的,如果对象更新影响到只读方法,那么只读方法也要定义成同步的。
如果一个线程必须等待一个对象状态发生变化,那么他应该在对象内部等待,而不是在外部。他可以通过调用一个被同步的方法,并让这个方法调用wait()。
每当一个方法返回某个对象的锁时,它应当调用notifyAll()来让等待队列中的其他线程有机会执行。
wait()和notify()/notifyAll()是Object类方法,而不是Thread类的方法。
针对wait()、notify()/notifyAll()使用旋锁(spin lock);
优先使用notifyAll()而不是notify();
按照固定的顺序获得多个对象锁,以避免死锁;
不要对上锁的对象改变它的引用;
不要滥用同步机制,避免无谓的同步控制。
十二:定时器示例
//示例3:
Java代码
1. package example;
2.
3. import java.util.Timer;
4. import java.util.TimerTask;
5.
6. public class TimerWork {
7. public static void main(String args[]) {
8. Timer tmr = new Timer();
9. tmr.schedule(new TimePrinter(), 0, 2000);
10. }
11. }
12.
13. class TimePrinter extends TimerTask {
14. int i = 0;
15.
16. public void run() {
17. System.out.println("No." + i++);
18. }
19. }
package example;
import java.util.Timer;
import java.util.TimerTask;
public class TimerWork {
public static void main(String args[]) {
Timer tmr = new Timer();
tmr.schedule(new TimePrinter(), 0, 2000);
}
}
class TimePrinter extends TimerTask {
int i = 0;
public void run() {
System.out.println("No." + i++);
}
}
//BounceThread
Java代码
1. package example;
2.
3. import java.awt.geom.*;
4.
5. public class Ball {
6. private static final int XSIZE = 15;
7. private static final int YSIZE = 15;
8. private double x = Math.random()*100;
9. private double y = Math.random()*100;
10. private