创建多线程(3种方法)

运行线程,使用start()方法,不要使用run()方法,如果使用run()方法创建的不是多线程

推荐使用接口的方式实现多线程,可以避免继承的局限性,便于共享资源

继承Thread类

public class Robbit extends Thread {

      @Override

      public void run() {

            for (int i = 0; i < 10000; i++) {

                  System.out.println("兔子跑了" + i + "步");

            }

      }



}

class Tortoise extends Thread {
      
      @Override

      public void run() {

            for (int i = 0; i < 10000; i++) {

                  System.out.println("乌龟跑了" + i + "步");

            }

      }
      
}

实现Runnable接口

public class Man implements Runnable {

      @Override

      public void run() {

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

                  System.out.println("男人跑了" + i + "步");

            }

      }

}

class Woman implements Runnable {

      @Override

      public void run() {

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

                  System.out.println("女人跑了" + i + "步");

            }

      }

}

通过Java提供的Callable接口(了解)

可以获取返回值,并且可以抛出异常,而Runnable是没有返回值的

具体的使用就是:

  1. 创建实现Callable接口的实现类,并实现里面的call()方法,这个call()方法就是线程的执行体,并且有返回值
  2. 借助ExecutorService获取Future对象

    ExecutorService ser = Executors.newFixedThreadPool(2);
    // 获取值
    Future<Integer> result1 = ser.submit(实现类的对象);
  3. 获取返回值:

     result.get();
  4. 停止服务

    ser.shutdownNow();
public class Cell {



      public static void main(String[] args) throws Exception {

            // 创建线程

            ExecutorService ser = Executors.newFixedThreadPool(2);

            Race tortoise = new Race("老不死", 1000);

            Race robbit = new Race("小兔子", 500);

            // 获取值

            Future<Integer> result1 = ser.submit(tortoise);

            Future<Integer> result2 = ser.submit(robbit);



            Thread.sleep(2000);

            

            //停止线程体循环

            tortoise.setFlag(false);

            robbit.setFlag(false);



            int num;

            int num2;

            num = result1.get();

            System.out.println("乌龟跑了" + num);

            num2 = result2.get();

            System.out.println("兔子" + num2);

            // 停止线程

            ser.shutdownNow();



      }



}



class Race implements Callable<Integer> {



      private String name;

      private long time;

      private boolean flag = true;

      private int step = 0;



      public Race() {

      }



      public Race(String name) {

            this.name = name;

      }



      public Race(String name, long time) {

            this.name = name;

            this.time = time;

      }



      @Override

      public Integer call() throws Exception {

            while (flag) {

                  Thread.sleep(time);

                  step++;

            }

            return step;

      }



      public String getName() {

            return name;

      }



      public void setName(String name) {

            this.name = name;

      }



      public long getTime() {

            return time;

      }



      public void setTime(long time) {

            this.time = time;

      }



      public boolean isFlag() {

            return flag;

      }



      public void setFlag(boolean flag) {

            this.flag = flag;

      }



      public int getStep() {

            return step;

      }



      public void setStep(int step) {

            this.step = step;

      }



}