创建多线程

多线程的创建有多种方式,分别是:

创建方式  优点 缺点
继承Thread类创建线程 使用简单 无法再继承其他类
实现Runnable接口创建线程 可以继承多个接口 比较复杂,无返回值
实现Callable接口 有返回值,可以继承多个接口 比较复杂

方法一:

 1 方法1:继承Thread类方法:
 2 /**
 3 1.自定义一个类,继承java.lang包下的Thread类
 4 2.重写run方法
 5 3.将要在线程中执行的代码编写在run方法中
 6 4.创建上面自定义类的对象
 7 5.调用start方法启动线程
 8  */
 9 class MyThread extends Thread{
10     @Override
11     public void run(){
12         for(int i=0; i<1000; i++){
13             System.out.println("a");
14         }
15     }
16 }
17 
18 public class ThreadTest01 {
19     public static void main(String[] args){
20         MyThread mt = new MyThread();
21         mt.start();
22         for(int i=0; i<1000; i++){
23             System.out.println("b");
24         }
25     }
26 }

方法二实现Runnable接口

 1 /*
 2  * 1.自定义一个类实现java.lang包下的Runnable接口
 3 2.重写run方法
 4 3.将要在线程中执行的代码编写在run方法中
 5 4.创建上面自定义类的对象
 6 5.创建Thread对象并将上面自定义类的对象作为参数传递给Thread的构造方法
 7 6.调用start方法启动线程
 8 */
 9 class MyRunnable implements Runnable {
10     @Override
11     public void run(){
12         for(int i=0; i<1000; i++){
13             System.out.println("a");
14         }
15     }
16 }
17 public class ThreadTest02 {
18 
19     public static void main(String[] args) {
20         // TODO Auto-generated method stub
21         MyRunnable mr = new MyRunnable();
22         Thread t = new Thread(mr); 
23         t.start();
24          for (int i = 0; i < 1000; i++) {
25                 System.out.println("1024");
26             }
27     }
28 
29 }

方法三使用Callable接口:

 1 package test;
 2 
 3 import java.util.concurrent.Callable;
 4 import java.util.concurrent.ExecutionException;
 5 import java.util.concurrent.ExecutorService;
 6 import java.util.concurrent.Executors;
 7 import java.util.concurrent.Future;
 8 
 9 /* 1.自定义一个类实现java.util.concurrent包下的Callable接口
10 2.重写call方法
11 3.将要在线程中执行的代码编写在call方法中
12 4.创建ExecutorService线程池
13 5.将自定义类的对象放入线程池里面
14 6.获取线程的返回结果
15 7.关闭线程池,不再接收新的线程,未执行完的线程不会被关闭*/
16 class MyCallable implements Callable<Integer>{
17      private int count;
18      
19      public MyCallable(int count){
20          this.count = count;
21      }
22     
23     @Override
24      public Integer call() throws Exception{
25         int sum=1;
26         if(count != 0){
27             for(int i=1; i<= count; i++){
28                 sum *= i;
29             }
30         }else{
31             sum = 0;
32         }
33           System.out.println(Thread.currentThread().getName() + "-----sum=" + sum);
34         return sum;
35      }
36 }
37 
38 
39 public class ThreadTest03 {
40 
41     public static void main(String[] args) throws InterruptedException, ExecutionException {
42         // TODO Auto-generated method stub
43         //创建线程池
44         //ExecutorService es = Executors.newFixedThreadPool(2);
45         //创建一个大小不固定的线程池,里面的线程会根据任务数量进行添加,资源耗费较大
46         ExecutorService es = Executors.newCachedThreadPool();
47         
48         Future<Integer> f1 = es.submit(new MyCallable(5));
49         Future<Integer> f2 = es.submit(new MyCallable(3));
50         
51         System.out.println(f1.get());
52         System.out.println(f2.get());
53 //        
54         es.isShutdown();
55         
56         //判断线程是否执行完毕
57 //        System.out.println("F1:" + f1.isDone());
58 //        System.out.println("F2:" + f2.isDone());
59         
60         System.out.println("main方法执行结束");
61     }
62 
63 }

方法1:继承Thread类方法:

/**

1.自定义一个类,继承java.lang包下的Thread

2.重写run方法

3.将要在线程中执行的代码编写在run方法中

4.创建上面自定义类的对象

5.调用start方法启动线程

 */

class MyThread extends Thread{

   @Override

   publicvoid run(){

      for(inti=0; i<1000; i++){

          System.out.println("a");

      }

   }

}

 

publicclass ThreadTest01 {

   publicstaticvoid main(String[] args){

      MyThread mt = new MyThread();

      mt.start();

      for(inti=0; i<1000; i++){

          System.out.println("b");

      }

   }

}

猜你喜欢

转载自www.cnblogs.com/feichangnice/p/10647721.html