说到线程,总会说到进程,在java中,进程和线程是操作系统调度的单位,两者都是操作系统的资源,进程实际是对线程的封装。在java中,有Process和Thread类分别实现进程和线程,这里不说进程,只是线程。
线程的实现由两种方式,可以extends Thread和implements Runnable。前者继承类,后者实现接口,总体而言有如下几点:
1.两者都重写run方法,都调用start方法执行。
2.两者的生命周期相同,包括创建---就绪---执行---(阻塞--就绪---执行)---终止。
3.java一个类只能继承一个类,可以实现多个接口,所以后者的扩展性更强,因而推荐使用接口。
4.用Runnable接口可以实现对同一资源的访问,共享而非独占。具体体现如下:
public class RunnableDeam implements Runnable {
private int apple;
public RunnableDeam(int a)
{
apple=a;
}
public void run() {
while(apple>0){
System.out.print(Thread.currentThread().getName());
System.out.print("现在有"+apple+"个,");
apple--;
//System.out.print(Thread.currentThread().getName());
System.out.println("还有"+apple+"个");
}
}
public static void main(String[] args) {
RunnableDeam r=new RunnableDeam(10);
Thread t1=new Thread(r,"t1");
Thread t2=new Thread(r,"t2");
Thread t3=new Thread(r,"t3");
t1.start();
t2.start();
t3.start();
}
}
执行结果为(先不考虑对错):
t2t3t1现在有10个,现在有10个,现在有10个,还有8个
t2还有9个
现在有7个,还有7个
还有6个
t2t1现在有6个,t3还有5个
现在有6个,t2现在有4个,还有3个
t2现在有3个,现在有5个,还有1个
t3还有2个
t2现在有1个,还有0个
现在有1个,还有4个
还有-1个
用Thread的方法如下:
public class threadDeam extends Thread {
private int apple;
private String name;
public threadDeam(int a,String na)
{
apple=a;
name=na;
}
public void run() {
while(apple>0){
System.out.print(name+"现在有"+apple+"个,");
apple--;
//System.out.print(Thread.currentThread().getName());
System.out.println("还有"+apple+"个");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
threadDeam t1=new threadDeam(2, "t1");
threadDeam t2=new threadDeam(2, "t2");
threadDeam t3=new threadDeam(2, "t3");
t1.start();
t2.start();
t3.start();
}
}
结果如下(先不看对错):
t3现在有2个,t2现在有2个,还有1个
t2现在有1个,还有0个
t1现在有2个,还有1个
还有1个
t3现在有1个,t1现在有1个,还有0个
还有0个
可以看到,实现runnable的例子,可以共享同一个数据,而后者不可以,但换种方式,让runnable的例子中线程实体不共用一个RunnableDeam r=new RunnableDeam(10),而是创建三个,就会出现同thread相同的结果。为了真正实现资源共享,我试着将共享数据设为static类型:
//继承Thread的例子
public class threadDeam extends Thread {
private static int apple;
private String name;
public threadDeam(int a,String na)
{
apple=a;
name=na;
}
public void run() {
while(apple>0){
System.out.print(name+"现在有"+apple+"个,");
apple--;
//System.out.print(Thread.currentThread().getName());
System.out.println("还有"+apple+"个");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
threadDeam t1=new threadDeam(2, "t1");
threadDeam t2=new threadDeam(2, "t2");
threadDeam t3=new threadDeam(2, "t3");
t1.start();
t2.start();
t3.start();
}
}
/*t1现在有2个,t2现在有2个,t3现在有2个,还有0个
还有1个
还有-1个*/
//实现Runnable的例子
public class RunnableDeam implements Runnable {
private static int apple;
public RunnableDeam(int a)
{
apple=a;
}
public void run() {
while(apple>0){
System.out.print(Thread.currentThread().getName());
System.out.print("现在有"+apple+"个,");
apple--;
//System.out.print(Thread.currentThread().getName());
System.out.println("还有"+apple+"个");
}
}
public static void main(String[] args) {
RunnableDeam r1=new RunnableDeam(3);
RunnableDeam r2=new RunnableDeam(3);
RunnableDeam r3=new RunnableDeam(3);
Thread t1=new Thread(r1,"t1");
Thread t2=new Thread(r2,"t2");
Thread t3=new Thread(r3,"t3");
t1.start();
t2.start();
t3.start();
}
}
/*
t2t1t3现在有3个,现在有3个,还有2个
现在有3个,还有0个
t2现在有0个,还有1个
还有-1个
*/
可以看到他们都是些了资源共享,因为static数据时类的数据而非某个对象的数据。
但在上面的输出中可以看出,结果并不正确,或者是并不是我们想要的。为了达到想要的目的,我们应该加入锁机制。我们以runnable为例子试验一下。
package DoExercise;
import java.util.concurrent.locks.ReentrantLock;
public class RunnableDeam implements Runnable {
private volatile static int apple;
private final ReentrantLock lock = new ReentrantLock();
public RunnableDeam(int a)
{
apple=a;
}
public void run() {
lock.lock();
if(apple>0){
System.out.print(Thread.currentThread().getName());
System.out.print("现在有"+apple+"个,");
apple--;
System.out.println("还有"+apple+"个");
lock.unlock();
}
}
public static void main(String[] args) {
RunnableDeam r1=new RunnableDeam(2);
Thread t1=new Thread(r1,"t1");
Thread t2=new Thread(r1,"t2");
Thread t3=new Thread(r1,"t3");
t1.start();
t2.start();
t3.start();
}
}
/*
*t1现在有2个,还有1个
*t2现在有1个,还有0个
*/
用synchronized机制也可实现:
package DoExercise;
public class RunnableDeam implements Runnable {
private volatile static int apple;
private static Object objLock=new Object();
public RunnableDeam(int a)
{
apple=a;
}
public void run() {
while(apple>0){
synchronized (objLock) {
if(apple>0){
System.out.print(Thread.currentThread().getName());
System.out.print("现在有"+apple+"个,");
apple--;
System.out.println("还有"+apple+"个");
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
RunnableDeam r1=new RunnableDeam(5);
Thread t1=new Thread(r1,"t1");
Thread t2=new Thread(r1,"t2");
Thread t3=new Thread(r1,"t3");
t1.start();
t2.start();
t3.start();
}
}
/*
t1现在有5个,还有4个
t3现在有4个,还有3个
t2现在有3个,还有2个
t3现在有2个,还有1个
t2现在有1个,还有0个
*/
可以看到,加了锁机制之后,就可以按照我们的逻辑执行。