多线程创建的三种方式

process 进程
thread 线程

1.程序、进程、线程

程序是指令和数据的有序集合,它没有任何运行的含义,是静态
进程是程序的一次执行,是动态的,是系统资源分配的单位
进程里面包含多个线程,一个进程里面包含至少一个线程,线程是CPU调用和执行的基本单位。

真正的多线程是有多个CPU同时执行多个线程哟(如服务器),现实中有很多模拟出来的多线程,只有一个CPU,在同那个一个时间点,CPU只能执行一个代码,
但由于CPU在多个线程之间切换的太快,就产生了同时执行的错觉。

2.线程的创建方式(有三种)

 

2.1第一种,继承Thread类


1.创建一个线程类,继承Thread类
2.重写run()方法
3.创建线程类对象,对象调用start()方法开启线程
注意:线程开启不一定立即执行,由CPU调度执行。

不建议使用,避免OOP单继承的局限性

 

/*
* 1.继承Thread类
* 2.重写run()方法
* 3.创建对象,调用start()方法,开启线程*/
public class TestThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("我自己开创的线程"+i);
        }
    }

    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        testThread.start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("main进程"+i);
        }
    }
}

 

 

2.2第二种,实现Runable接口


1.创建一个线程类,实现Runnable接口
2.实现run()方法
3.创建线程类对象,new Thread(对象).start()来启动线程(代理)(自己创建的线程无start()方法)

推荐使用:避免单继承的局限性,灵活方便,方便同一个对象被多个线程所使用。

public class TestThread03 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("自创线程"+i);
        }
    }

    public static void main(String[] args) {
        TestThread03 testThread03 = new TestThread03();
        new Thread(testThread03).start();//这里与第一种方法不同,用到了静态代理模式

        for (int i = 0; i < 500; i++) {
            System.out.println("main线程"+i);
        }

    }
}

 

当多个线程在抢占同一个资源时,如果不加以处理,会出现多个线程同时使用同一个资源的情况,发生资源数据紊乱了。--线程并发问题

2.3创建线程的第三种方式:利用callable接口


* 1.继承callable接口
* 2.重写call()方法,含返回值类型和抛出异常
* 3.创建类对象
* 4.创建执行任务 ExecutorService service = Executors.newFixedThreadPool(3);
* 5。提交执行 Future<Boolean> f1 = service.submit(t1);
* 6.获取结果 boolean rs1 = f1.get();
* 7.关闭服务 service.shutdownNow();

/*
* 创建线程的第三种方式:利用callable接口
* 1.继承callable接口
* 2.重写call()方法,含返回值类型和抛出异常
* 3.创建类对象
* 4.创建执行任务 ExecutorService service = Executors.newFixedThreadPool(3);
* 5。提交执行  Future<Boolean> f1 = service.submit(t1);
* 6.获取结果  boolean rs1 = f1.get();
* 7.关闭服务 service.shutdownNow();
* */

import java.util.concurrent.*;

public class TestCallable implements Callable {
    @Override
    public Boolean call() throws Exception {
        System.out.println("自创线程开始运行。。。。。");
        return true;
    }

    public static void main(String[] args) {
        TestCallable t1 = new TestCallable();
        TestCallable t2 = new TestCallable();
        TestCallable t3 = new TestCallable();
        //创建执行服务
      ExecutorService service = Executors.newFixedThreadPool(3);
      //提交执行
      Future<Boolean> f1 = service.submit(t1);
      Future<Boolean> f2 = service.submit(t2);
      Future<Boolean> f3 = service.submit(t3);

      //获取结果
        try {
            boolean rs1 = f1.get();
            boolean rs2 = f2.get();
            boolean rs3 = f3.get();
            System.out.println(rs1+" "+rs2+" "+rs3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

//        关闭服务
        service.shutdownNow();



    }
}

 也可以这样启动线程:

package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestCallable3 implements Callable<Integer> {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> future = new FutureTask<Integer>(new TestCallable3());
        new Thread(future).start();
        Integer integer = future.get();
        System.out.println(integer);

    }
    @Override
    public Integer call() throws Exception {
        System.out.println("线程开始运行。。。。。。。");
        return 100;
    }
}

 

posted @ 2020-07-21 11:23  DannyBoy~  阅读(1535)  评论(0编辑  收藏  举报