认识多线程

本章目标
了解进程与线程的区别
掌握Java线程的两种实现方式及其区别
了解线程的操作状态

 

进程与线程
进程是程序的一次动态执行过程,它经历了从代码加载、执行到执行完毕的一个完整过程,这个过程也是进程本身从产生、发展到最终消亡的过程。
多线程是实现并发机制的一种有效手段。进程和线程一样,都是实现并发的一个基本单位。
认识多线程
 

 

Java中线程的实现
在Java中要想实现多线程代码有两种手段:
——一种是继承Thread类
——另一种就是实现Runnable接口

 

继承Thread类
Thread类是在java.lang包中定义的,一个类只要继承了Thread类,此类就称为多线程操作类。在Thread子类之中,必须明确的覆写Thread类中的run()方法,此方法为线程的主体。
多线程的定义语法:
——class 类名称 extends Thread{  // 继承Thread类
  属性… ;   // 类中定义属性
  方法… ;   // 类中定义方法
   // 覆写Thread类中的run()方法,此方法是线程的主体
  public void run(){
  线程主体;
  }
  }

 

继承Thread类实现多线程

class MyThread extends Thread{//继承 Thread 类
	private String name;//在类中定义一个属性
	public MyThread(String name){//通过构造方法设置属性内容
		this.name = name;//为 name 属性赋值
	}
	public void run(){//覆写 Thread 类中的 run() 方法
		for(int i=0; i<10; i++){//循环 10 次输出
			System.out.println(name +"运行,i = "+i);
		}
	}
}
public class ThreadDemo01 {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程A");//实例化对象
		MyThread mt2 = new MyThread("线程B");//实例化对象
		mt1.run();//调用线程主体
		mt2.run();//调用线程主体
	}
/* 结果:
 * 线程A运行,i = 0
 * 线程A运行,i = 1
 * 线程A运行,i = 2
 * 线程A运行,i = 3
 * 线程A运行,i = 4
 * 线程A运行,i = 5
 * 线程A运行,i = 6
 * 线程A运行,i = 7
 * 线程A运行,i = 8
 * 线程A运行,i = 9
 * 线程B运行,i = 0
 * 线程B运行,i = 1
 * 线程B运行,i = 2
 * 线程B运行,i = 3
 * 线程B运行,i = 4
 * 线程B运行,i = 5
 * 线程B运行,i = 6
 * 线程B运行,i = 7
 * 线程B运行,i = 8
 * 线程B运行,i = 9
 * */	
}

 

class MyThread extends Thread{//继承 Thread 类
	private String name;//在类中定义一个属性
	public MyThread(String name){//通过构造方法设置属性内容
		this.name = name;//为 name 属性赋值
	}
	public void run(){//覆写 Thread 类中的 run() 方法
		for(int i=0; i<10; i++){//循环 10 次输出
			System.out.println(name +"运行,i = "+i);
		}
	}
}
public class ThreadDemo02 {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程A");//实例化对象
		MyThread mt2 = new MyThread("线程B");//实例化对象
		mt1.start();//调用线程主体
		mt2.start();//调用线程主体
	}
/* 结果:
 * 线程A运行,i = 0
 * 线程B运行,i = 0
 * 线程B运行,i = 1
 * 线程B运行,i = 2
 * 线程B运行,i = 3
 * 线程B运行,i = 4
 * 线程B运行,i = 5
 * 线程A运行,i = 1
 * 线程B运行,i = 6
 * 线程A运行,i = 2
 * 线程B运行,i = 7
 * 线程A运行,i = 3
 * 线程B运行,i = 8
 * 线程A运行,i = 4
 * 线程B运行,i = 9
 * 线程A运行,i = 5
 * 线程A运行,i = 6
 * 线程A运行,i = 7
 * 线程A运行,i = 8
 * 线程A运行,i = 9
 * */	
}

 

class MyThread extends Thread{//继承 Thread 类
	private String name;//在类中定义一个属性
	public MyThread(String name){//通过构造方法设置属性内容
		this.name = name;//为 name 属性赋值
	}
	public void run(){//覆写 Thread 类中的 run() 方法
		for(int i=0; i<10; i++){//循环 10 次输出
			System.out.println(name +"运行,i = "+i);
		}
	}
}
public class ThreadDemo03 {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程A");//实例化对象
		mt1.start();//启用多线程
		mt1.start();//错误,第 2 次调用 start() 方法
	}
/* 结果:
 * 线程A运行,i = 0
 * 线程A运行,i = 1
 * 线程A运行,i = 2
 * 线程A运行,i = 3
 * 线程A运行,i = 4
 * 线程A运行,i = 5
 * 线程A运行,i = 6
 * 线程A运行,i = 7
 * 线程A运行,i = 8
 * 线程A运行,i = 9
 * Exception in thread "main" java.lang.IllegalThreadStateException
 * 	at java.lang.Thread.start(Thread.java:595)
 * 	at J030901.ThreadDemo03.ThreadDemo03.main(ThreadDemo03.java:18)
 * */	
}

 

启动线程
如果要想正确的启动线程,是不能直接调用run()方法的,应该调用从Thread类中继承而来的start()方法,才可以启动线程。
——mt1.start();  // 启动多线程
——mt2.start();  // 启动多线程

 
start方法定义
  public synchronized void start() {
  if (threadStatus != 0)
  throw new IllegalThreadStateException();
  ...
  start0();
  ...
  }
  private native void start0();
  从以上的代码中可以发现,在一个类中的start()方法调用时可能会抛出“IllegalThreadStateException”的异常,一般在重复调用start()方法的时候会抛出这个异常。而且实际上此处真正调用的是start0()方法,此方法在声明处使用了native关键字声明,此关键字表示调用本机的操作系统函数,因为多线程的实现需要依靠底层操作系统支持。 

 

 实现Runnable接口
在Java中也可以通过实现Runnable接口的方式实现多线程,Runnable接口中只定义了一个抽象方法:
——public void run() ;
通过Runnable接口实现多线程:
——class 类名称 implements Runnable{  // 实现Runnable接口
  属性… ;   // 类中定义属性
  方法… ;   // 类中定义方法
  public void run(){              // 覆写Runnable接口里的run()方法  线程主体 ;
  }
  }

 

实现Runnable接口
class MyThread implements Runnable {  // 实现Runnable接口
  private String name;  // 在类中定义一个属性
  public MyThread(String name) {  // 通过构造方法设置属性内容
  this.name = name;
  }
  public void run() {  // 覆写Runnable接口中的run()方法
  for (int i = 0; i < 10; i++) {
  System.out.println(name + "运行,i = " + i);
  }
  }
};

 

启动Runnable实现的多线程
以上的代码是通过实现Runnable接口实现的多线程,但是这样一来就会有新的问题产生了,从之前的代码中可以清楚的知道,要想启动一个多线程必须要使用start()方法完成,如果继承了Thread类,则可以直接从Thread类中使用start()方法,但是现在实现的是Runnable接口,那么该如何启动多线程呢?实际上此时,还是要依靠Thread类完成启动,在Thread类中提供了以下的两个构造方法:
——public Thread(Runnable target)
——public Thread(Runnable target,String name)
这两个构造方法都可以接收Runnable的子类实例对象。所以就可以依靠此点启动多线程。

 

使用Thread类启动多线程

class MyThread implements Runnable{//实现 Runnable 类
	private String name;//在类中定义一个属性
	public MyThread(String name){//通过构造方法设置属性内容
		this.name = name;//为 name 属性赋值
	}
	public void run(){//覆写 Runnable 类中的 run() 方法
		for(int i=0; i<10; i++){//循环 10 次输出
			System.out.println(name +"运行,i = "+i);
		}
	}
}
public class RunnableDemo01 {
	public static void main(String[] args) {
		MyThread my1 = new MyThread("线程A");//实例化 Runnable 子类对象
		MyThread my2 = new MyThread("线程B");//实例化 Runnable 子类对象
		Thread t1 = new Thread(my1);//实例化 Thread 类对象
		Thread t2 = new Thread(my2);//实例化 Thread 类对象
		t1.start();//启动线程
		t2.start();//启动线程
	}
/* 结果:
 * 线程A运行,i = 0
 * 线程B运行,i = 0
 * 线程A运行,i = 1
 * 线程B运行,i = 1
 * 线程A运行,i = 2
 * 线程B运行,i = 2
 * 线程A运行,i = 3
 * 线程A运行,i = 4
 * 线程A运行,i = 5
 * 线程A运行,i = 6
 * 线程A运行,i = 7
 * 线程A运行,i = 8
 * 线程A运行,i = 9
 * 线程B运行,i = 3
 * 线程B运行,i = 4
 * 线程B运行,i = 5
 * 线程B运行,i = 6
 * 线程B运行,i = 7
 * 线程B运行,i = 8
 * 线程B运行,i = 9
 * */	
}

 

Thread类和Runnable接口的联系
Thread类的定义
——public class Thread extends Object implements Runnable
Thread类的部分定义
认识多线程
 

 Thread类和Runnable接口的区别
实际上Thread类和Runnable接口之间在使用上也是有所区别的,如果一个类继承Thread类,则不适合于多个线程共享资源,而实现了Runnable接口,则可以方便的实现资源的共享。

 

继承Thread类不能资源共享

class MyThread extends Thread{//继承 Thread 类
	private int ticket = 5;//一共五张票
	public void run(){//覆写 run() 方法
		for(int i=0; i<100; i++){//超出票数的循环
			if(ticket>0){//判断是否有剩余票
				System.out.println("卖票:ticket = "+ticket--);
			}
		}
	}
}
public class ThreadDemo04 {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread();//定义线程对象
		MyThread mt2 = new MyThread();//定义线程对象
		MyThread mt3 = new MyThread();//定义线程对象
		mt1.start();//启动第 1 个线程
		mt2.start();//启动第 2 个线程
		mt3.start();//启动第 3 个线程
	}
/* 结果:
 * 卖票:ticket = 5
 * 卖票:ticket = 5
 * 卖票:ticket = 4
 * 卖票:ticket = 5
 * 卖票:ticket = 3
 * 卖票:ticket = 4
 * 卖票:ticket = 2
 * 卖票:ticket = 1
 * 卖票:ticket = 4
 * 卖票:ticket = 3
 * 卖票:ticket = 2
 * 卖票:ticket = 1
 * 卖票:ticket = 3
 * 卖票:ticket = 2
 * 卖票:ticket = 1
 * */	
}

 

实现Runnable接口可以资源共享

class MyThread implements Runnable{//继承 Thread 类
	private int ticket = 5;//一共五张票
	public void run(){//覆写 run() 方法
		for(int i=0; i<100; i++){//超出票数的循环
			if(ticket>0){//判断是否有剩余票
				System.out.println("卖票:ticket = "+ticket--);
			}
		}
	}
}
public class RunnableDemo02 {
	public static void main(String[] args) {
		MyThread my = new  MyThread();
		new Thread(my).start();//启动 3 个线程
		new Thread(my).start();//启动 3 个线程
		new Thread(my).start();//启动 3 个线程
	}
/* 结果:
 * 卖票:ticket = 5
 * 卖票:ticket = 4
 * 卖票:ticket = 3
 * 卖票:ticket = 2
 * 卖票:ticket = 1
 * */	
}

 总结
可见,实现Runnable接口相对于继承Thread类来说,有如下显著的优势:
——(1)、适合多个相同程序代码的线程去处理同一资源的情况。
——(2)、可以避免由于Java的单继承特性带来的局限。
——(3)、增强了程序的健壮性,代码能够被多个线程共享,代码与数据是独立的。  

 

 线程的状态
要想实现多线程,必须在主线程中创建新的线程对象。任何线程一般具有五种状态,即创建、就绪、运行、阻塞、终止。
认识多线程