Java多线程的创建及启动

文章出处:http://www.cnblogs.com/lwbqqyumidi/p/3804883.html

        Java中线程的创建常见有如三种基本形式。

        1.继承Thread类,重写run()方法

        run()方法的方法体代表了线程需要完成的任务,称之为线程执行体。

        通过调用start()方法执行该线程。start()方法的调用后并不是立即执行多线程代码,而是使得该线程变为可运行态(Runnable),什么时候运行是由操作系统决定的。

        多线程程序是乱序执行,只有乱序执行的代码才有必要设计为多线程。

复制代码
 1 class MyThread extends Thread {
 2     
 3     private int i = 0;
 4 
 5     @Override
 6     public void run() {
 7         for (i = 0; i < 100; i++) {
 8             System.out.println(Thread.currentThread().getName() + " " + i);
 9         }
10     }
11 }
复制代码
复制代码
 1 public class ThreadTest {
 2 
 3     public static void main(String[] args) {
 4         for (int i = 0; i < 100; i++) {
 5             System.out.println(Thread.currentThread().getName() + " " + i);
 6             if (i == 30) {
 7                 Thread myThread1 = new MyThread();     // 创建一个新的线程  myThread1  此线程进入新建状态
 8                 Thread myThread2 = new MyThread();     // 创建一个新的线程 myThread2 此线程进入新建状态
 9                 myThread1.start();                     // 调用start()方法使得线程进入就绪状态
10                 myThread2.start();                     // 调用start()方法使得线程进入就绪状态
11             }
12         }
13     }
14 }
复制代码

       一个线程的start方法只能调用一次,重复调用会出现java.lang.IllegalThreadStateException异常。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Thread1 mTh1=new Thread1("A");  
  2. Thread1 mTh2=mTh1;  
  3. mTh1.start();  
  4. 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)



        2.实现Runnable接口,重写该接口的run()方法。

        创建Runnable实现类的实例,并以此实例作为Thread类的target来创建Thread对象,该Thread对象才是真正的线程对象。

复制代码
 1 class MyRunnable implements Runnable {
 2     private int i = 0;
 3 
 4     @Override
 5     public void run() {
 6         for (i = 0; i < 100; i++) {
 7             System.out.println(Thread.currentThread().getName() + " " + i);
 8         }
 9     }
10 }
复制代码
复制代码
 1 public class ThreadTest {
 2 
 3     public static void main(String[] args) {
 4         for (int i = 0; i < 100; i++) {
 5             System.out.println(Thread.currentThread().getName() + " " + i);
 6             if (i == 30) {
 7                 Runnable myRunnable = new MyRunnable(); // 创建一个Runnable实现类的对象
 8                 Thread thread1 = new Thread(myRunnable); // 将myRunnable作为Thread target创建新的线程
 9                 Thread thread2 = new Thread(myRunnable);
10                 thread1.start(); // 调用start()方法使得线程进入就绪状态
11                 thread2.start();
12             }
13         }
14     }
15 }
复制代码



        关于以上两种线程创建的方法的特殊例子:

复制代码
 1 public class ThreadTest {
 2 
 3     public static void main(String[] args) {
 4         for (int i = 0; i < 100; i++) {
 5             System.out.println(Thread.currentThread().getName() + " " + i);
 6             if (i == 30) {
 7                 Runnable myRunnable = new MyRunnable();
 8                 Thread thread = new MyThread(myRunnable);
 9                 thread.start();
10             }
11         }
12     }
13 }
14 
15 class MyRunnable implements Runnable {
16     private int i = 0;
17 
18     @Override
19     public void run() {
20         System.out.println("in MyRunnable run");
21         for (i = 0; i < 100; i++) {
22             System.out.println(Thread.currentThread().getName() + " " + i);
23         }
24     }
25 }
26 
27 class MyThread extends Thread {
28 
29     private int i = 0;
30     
31     public MyThread(Runnable runnable){
32         super(runnable);
33     }
34 
35     @Override
36     public void run() {
37         System.out.println("in MyThread run");
38         for (i = 0; i < 100; i++) {
39             System.out.println(Thread.currentThread().getName() + " " + i);
40         }
41     }
42 }
复制代码

        同样的,与实现Runnable接口创建线程方式相似,不同的地方在于

1 Thread thread = new MyThread(myRunnable);

        那么这种方式可以顺利创建出一个新的线程么?答案是肯定的。

        至于此时的线程执行体到底是MyRunnable接口中的run()方法,还是MyThread类中的run()方法呢?通过输出我们知道线程执行体是MyThread类中的run()方法。

        原因很简单,因为Thread类本身也是实现了Runnable接口,而run()方法最先是在Runnable接口中定义的方法。

1 public interface Runnable {
2    
3     public abstract void run();
4     
5 }

        我们看一下Thread类中对Runnable接口中run()方法的实现:

  @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

        当执行到Thread类中的run()方法时,会首先判断target是否存在,存在则执行target中的run()方法,也就是实现了Runnable接口并重写了run()方法的类中的run()方法。

        但是上述给到的列子中,由于多态的存在,根本就没有执行到Thread类中的run()方法,而是直接先执行了运行时类型即MyThread类中的run()方法。


        3.使用Callable和Future接口创建线程。1.创建Callable接口的实现类,实现call()方法;2.使用FutureTask类包装Callable实现类;3.以FutureTask对象作为Thread的target来创建线程。

复制代码
 1 public class ThreadTest {
 2 
 3     public static void main(String[] args) {
 4 
 5         Callable<Integer> myCallable = new MyCallable();    // 创建MyCallable对象
 6         FutureTask<Integer> ft = new FutureTask<Integer>(myCallable); //使用FutureTask来包装MyCallable对象
 7 
 8         for (int i = 0; i < 100; i++) {
 9             System.out.println(Thread.currentThread().getName() + " " + i);
10             if (i == 30) {
11                 Thread thread = new Thread(ft);   //FutureTask对象作为Thread对象的target创建新的线程
12                 thread.start();                      //线程进入到就绪状态
13             }
14         }
15 
16         System.out.println("主线程for循环执行完毕..");
17         
18         try {
19             int sum = ft.get();            //取得新创建的新线程中的call()方法返回的结果
20             System.out.println("sum = " + sum);
21         } catch (InterruptedException e) {
22             e.printStackTrace();
23         } catch (ExecutionException e) {
24             e.printStackTrace();
25         }
26 
27     }
28 }
29 
30 
31 class MyCallable implements Callable<Integer> {
32     private int i = 0;
33 
34     // 与run()方法不同的是,call()方法具有返回值
35     @Override
36     public Integer call() {
37         int sum = 0;
38         for (; i < 100; i++) {
39             System.out.println(Thread.currentThread().getName() + " " + i);
40             sum += i;
41         }
42         return sum;
43     }
44 
45 }
复制代码

        首先,我们发现,在实现Callable接口中,此时不再是run()方法了,而是call()方法,此call()方法作为线程执行体,同时还具有返回值!

        在创建新的线程时,是通过FutureTask来包装MyCallable对象,同时作为了Thread对象的target。

那么看下FutureTask类的定义:

1 public class FutureTask<V> implements RunnableFuture<V> {
2     
3     //....
4     
5 }
1 public interface RunnableFuture<V> extends Runnable, Future<V> {
2     
3     void run();
4     
5 }

FutureTask类实际上是同时实现了Runnable和Future接口,由此才使得其具有Future和Runnable双重特性。通过Runnable特性,可以作为Thread对象的target,而Future特性,使得其可以取得新创建线程中的call()方法的返回值。

执行下此程序,我们发现sum = 4950永远都是最后输出的。而“主线程for循环执行完毕..”则很可能是在子线程循环中间输出。由CPU的线程调度机制,我们知道,“主线程for循环执行完毕..”的输出时机是没有任何问题的,那么为什么sum =4950会永远最后输出呢?

原因在于通过ft.get()方法获取子线程call()方法的返回值时,当子线程此方法还未执行完毕,ft.get()方法会一直阻塞,直到call()方法执行完毕才能取到返回值。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值