Java线程的创建方式

线程创建方式

  1. 直接继承Thread
  2. 实现Runnable接口
  3. 实现Callable接口

方式一

线程的创建

package com.jimory.thread.create;

/**
* 线程创建(一) 
* 1、继承Thread类
* 2、重写run方法 , run方法里是线程体
* 
* 线程使用
* 1、创建类对象
* 2、调用start方法(注意是 对象.start() 不能用对象.run()方法)
* 
* 模拟龟兔赛跑
* @author Jimory
*
*/
public class Rabbit extends Thread{
    @Override
    public void run() {
        for(int i=1;i<=100;i++){
            System.out.println("兔子跑了"+i+"步");
        }
    }
}

class Toritor extends Thread{
    @Override
    public void run() {
        for(int i=1;i<=100;i++){
            System.out.println("乌龟跑了"+i+"步");
        }
   }
}

线程的使用

package com.jimory.thread.create;

public class ThreadTest01 {

    /**
     * 这里有三个线程,main方法也是一个线程,俩个start()俩个线程,共三个
     * 或者可以说是五个线程,另外俩个是异常和后台jc
     * @param args
     */
    public static void main(String[] args) {
        Rabbit ra = new Rabbit();
        Toritor to = new Toritor();
        ra.start(); //启动线程,注意不能通过调用run方法
        to.start();

        for(int i=1;i<=100;i++){
            System.out.println("main方法"+i+"步");
        }
   }
}

方式二
优点:解决了方式一的单继承问题,比如说某个类必须继承另一个类,由于单继承问题所以不能继承Thread类,通过方式二的接口方式(这里用到了静态代理),推介使用

线程的创建

 package com.jimory.thread.create;

 /**
 * 线程创建(二)
 * 1、实现Runnable接口
 * 2、重写run方法
 * 
 * 线程使用
 * 1、创建真实角色
 * 2、创建代理对象 + 引用(引用真实角色对象)
 * 2、代理对象.start()
 * @author Jimory
 *
 */
 public class Rabbit02 implements Runnable{

     @Override
     public void run() {
         for(int i=0;i<100;i++){
             System.out.println("hahahahhahaahha"+i);
         }

     }

线程的使用

 package com.jimory.thread.create;

 public class RunnableTest {
     public static void main(String[] args) {
         //创建真实角色
         Rabbit02 ra2 = new Rabbit02();
         //创建代理角色 + 引用
         Thread th = new Thread(ra2, "路人甲");
         //代理角色启动线程
         th.start();

         for(int i=0;i<100;i++){
             System.out.println("eeee"+i);
         }
     }

方式三

 package com.jimory.thread.create;

 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;

 /**
 * 线程创建 较其他俩种创建方式,Callable接口可以返回值,可以抛出异常
 * 1、类要实现Callable接口
 * 2、ExecutorService ser = Executors.newFixedThreadPool(1); 开启几个线程就pool就写几
 * 
 * 线程关闭
 * ser.shutdownNow();
 * @author Jimory
 *
 */
 public class Call{

     /**
      * 线程创建、关闭、获取返回值实例
      */
     public static void main(String[] args) throws InterruptedException, ExecutionException {
         //创建线程
         ExecutorService ser = Executors.newFixedThreadPool(1);
         Race race = new Race();
         //获取值
         Future<Integer> result = ser.submit(race);
         int num = result.get();
         System.out.println(num);
         //线程关闭
         ser.shutdownNow();

     }

 }

 class Race implements Callable<Integer>{

     @Override
     public Integer call() throws Exception {

         return 1000;

猜你喜欢

转载自blog.csdn.net/Jimory/article/details/51520602