一、线程的生命状态
我们查看Thread.State类可以发现,线程的生命状态分成NEW,RUNNABLE,BLOCKED,WAITING,TIME_WATING,TEMINATED几种状态,这几种状态的具体含义如下。
(1)NEW:这种状态表示线程刚刚创建,还没有开始执行,那么什么时候才开始执行呢?等到调用start()方法的时候线程就开始执行了。在这里需要注意的是只要线程经过了NEW状态就不可能再次回到NEW状态。
(2)RUNNALE:当调用start()方法后,也就是线程进入开始状态后,所有资源就准备好了,这个时候线程就处于RUNNABLE状态。
(3)BLOCKED:在线程执行的过程中,如果遇到了synchronized同步块,这个时候线程就会进入BLOCKED状态,线程暂停,直到获取到线程请求的锁。
(4)WATING,TIME_WATING:这两种状态都表示线程是处于等待状态,只是前面的等待有可能是无限制的等待,后面的等待是有时间限制的等待,比如wait()等待notify()方法。
(5)TEMINATED:这个时候线程处于终止状态,注意:处于终止状态的线程不可能再回到RUNNABLE状态。
二、线程的基本操作
(1)新建线程
在java中创建线程可以通过实现Runnable接口和继承Thread的方式来实现。具体实现的代码如下:
例1、通过继承Thread的方式来实现多线程。
/**
* 通过集成Thread来创建线程
*/
class MutliThread extends Thread{
private int ticket = 10 ;//每个线程拥有10张票
public MutliThread(){}
public MutliThread(String name){
super(name);
}
@Override
public void run(){
while(ticket > 0 ){
System.out.println(ticket-- +"is saled,"+Thread.currentThread().getName());
}
}
}
public static void main(String[] args) {
MutliThread mutliThread1 = new MutliThread("thread 1");
MutliThread mutliThread2 = new MutliThread("thread 2");
mutliThread1.start();
mutliThread2.start();
}
运行结果如下:
10is saled,thread 1
9is saled,thread 1
8is saled,thread 1
7is saled,thread 1
6is saled,thread 1
5is saled,thread 1
4is saled,thread 1
3is saled,thread 1
2is saled,thread 1
1is saled,thread 1
10is saled,thread 2
9is saled,thread 2
8is saled,thread 2
7is saled,thread 2
6is saled,thread 2
5is saled,thread 2
4is saled,thread 2
3is saled,thread 2
2is saled,thread 2
1is saled,thread 2
通过上面的代码及执行结果,我们可以看出多线程得以实现,同时每个线程都是用自己的变量,变量间不相互干扰,也就是每个线程都有自己的10张票进行销售。
例2、实现Runnable来实现多线程
如果我们去查看Thread的源码,可以发现在对Thread进行实例化的时候,需要传入Runnable对象,并且在调用run()方法的时候,其实最终也是调用的Runnable的run()方法,如果没有传入Runnable对象进行实例化,要么就要多线程类重写Thread类的run()方法,要么线程执行的时候将会没有任何的执行结果。
/**
* 通过实现Runnable来实现多线程
*/
class MutliRunnable implements Runnable{
private int ticket = 10;
private String name;
public MutliRunnable(){}
public MutliRunnable(String name){
this.name = name;
}
@Override
public void run() {
while(ticket > 0 ){
System.out.println(ticket-- +"is saled,"+name);
}
}
}
public static void main(String[] args) {
MutliRunnable mutliRunnable1 = new MutliRunnable("Runnable 1");
MutliRunnable mutliRunnable2 = new MutliRunnable("Runnable 2");
Thread t1 = new Thread(mutliRunnable1);
Thread t2 = new Thread(mutliRunnable2);
t1.start();
t2.start();
}
程序执行结果如下:
10is saled,Runnable 2
10is saled,Runnable 1
9is saled,Runnable 2
8is saled,Runnable 2
7is saled,Runnable 2
6is saled,Runnable 2
5is saled,Runnable 2
4is saled,Runnable 2
3is saled,Runnable 2
2is saled,Runnable 2
1is saled,Runnable 2
9is saled,Runnable 1
8is saled,Runnable 1
7is saled,Runnable 1
6is saled,Runnable 1
5is saled,Runnable 1
4is saled,Runnable 1
3is saled,Runnable 1
2is saled,Runnable 1
1is saled,Runnable 1
从上面的执行结果我们可以看见,和例1的执行结果是相同的。
在此我们就有了一个疑问,如果我们要多个线程共享一个数据,这个怎么办呢?针对这个需求,我们只能使用Runnable的方式来实现。,这也是前面两种方式的区别所在。
例3、多个线程共享数据
/**
* 多个线程共享资源
*/
class MutliRunnable implements Runnable{
private int ticket = 10;
private String name;
public MutliRunnable(){}
public MutliRunnable(String name){
this.name = name;
}
@Override
public void run() {
while(ticket > 0 ){
System.out.println(ticket-- +"is saled,"+Thread.currentThread().getName());
}
}
}
程序执行结果:10is saled,Thread 1
9is saled,Thread 2
8is saled,Thread 1
7is saled,Thread 2
6is saled,Thread 1
4is saled,Thread 1
3is saled,Thread 1
2is saled,Thread 1
1is saled,Thread 1
5is saled,Thread 2
通过上面的结果我们可以看见两个线程对10张票进行了随机销售。