3.java多线程的实现
3.1 扩展java.lang.Thread类
这里继承Thread类的方法是比较常用的一种,如果说你只是想起一条线程。没有什么其它特殊的要求,那么可以使用Thread.(推荐使用Runable,后头会说明为什么)。下面来看一个简单的实例
- package com.multithread.learning;
- /**
- *@functon 多线程学习
- *@author 林炳文
- *@time 2015.3.9
- */
- class Thread1 extends Thread{
- private String name;
- public Thread1(String name) {
- this.name=name;
- }
- public void run() {
- for (int i = 0; i < 5; i++) {
- System.out.println(name + "运行 : " + i);
- try {
- sleep((int) Math.random() * 10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Thread1 mTh1=new Thread1("A");
- Thread1 mTh2=new Thread1("B");
- mTh1.start();
- mTh2.start();
- }
- }
输出:
A运行 : 0
B运行 : 0
A运行 : 1
A运行 : 2
A运行 : 3
A运行 : 4
B运行 : 1
B运行 : 2
B运行 : 3
B运行 : 4
再运行一下:
A运行 : 0
B运行 : 0
B运行 : 1
B运行 : 2
B运行 : 3
B运行 : 4
A运行 : 1
A运行 : 2
A运行 : 3
A运行 : 4
说明:
程序启动运行main时候,java虚拟机启动一个进程,主线程main在main()调用时候被创建。随着调用MitiSay的两个对象的start方法,另外两个线程也启动了,这样,整个应用就在多线程下运行。
注意:start()方法的调用后并不是立即执行多线程代码,而是使得该线程变为可运行态(Runnable),什么时候运行是由操作系统决定的。
从程序运行的结果可以发现,多线程程序是乱序执行。因此,只有乱序执行的代码才有必要设计为多线程。
Thread.sleep()方法调用目的是不让当前线程独自霸占该进程所获取的CPU资源,以留出一定时间给其他线程执行的机会。
实际上所有的多线程代码执行顺序都是不确定的,每次执行的结果都是随机的。
但是start方法重复调用的话,会出现java.lang.IllegalThreadStateException异常。
- Thread1 mTh1=new Thread1("A");
- Thread1 mTh2=mTh1;
- mTh1.start();
- mTh2.start();
输出:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at com.multithread.learning.Main.main(Main.java:31)
A运行 : 0
A运行 : 1
A运行 : 2
A运行 : 3
A运行 : 4
3.2 实现java.lang.Runnable接口
采用Runnable也是非常常见的一种,我们只需要重写run方法即可。下面也来看个实例。
- /**
- *@functon 多线程学习
- *@author 林炳文
- *@time 2015.3.9
- */
- package com.multithread.runnable;
- class Thread2 implements Runnable{
- private String name;
- public Thread2(String name) {
- this.name=name;
- }
- @Override
- public void run() {
- for (int i = 0; i < 5; i++) {
- System.out.println(name + "运行 : " + i);
- try {
- Thread.sleep((int) Math.random() * 10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- new Thread(new Thread2("C")).start();
- new Thread(new Thread2("D")).start();
- }
- }
输出:
C运行 : 0
D运行 : 0
D运行 : 1
C运行 : 1
D运行 : 2
C运行 : 2
D运行 : 3
C运行 : 3
D运行 : 4
C运行 : 4
说明:
Thread2类通过实现Runnable接口,使得该类有了多线程类的特征。run()方法是多线程程序的一个约定。所有的多线程代码都在run方法里面。Thread类实际上也是实现了Runnable接口的类。
在启动的多线程的时候,需要先通过Thread类的构造方法Thread(Runnable target) 构造出对象,然后调用Thread对象的start()方法来运行多线程代码。
实际上所有的多线程代码都是通过运行Thread的start()方法来运行的。因此,不管是扩展Thread类还是实现Runnable接口来实现多线程,最终还是通过Thread的对象的API来控制线程的,熟悉Thread类的API是进行多线程编程的基础。