多线程——程序、进程、线程及创建线程的5种方法

程序、进程、线程概念

程序可执行文件,如,QQ.exe、项目jar包、war包、Linux中.sh
进程进程是资源分配的基本单位,程序运行会产生一个进程,没有限制的话一个程序可以多运行几个,便产生多个进程;我们一般打开的QQ、QQ音乐、office、浏览器都是相应的程序运行产生的进程。
线程CPU调度的基本单位,一个程序中不听的执行路径。如:QQ音乐在听歌的同时,歌词在在跟着自动往上滑。音乐播放是一个线程、歌词滑动是一个线程,各司其职,相互关联。

创建线程的5种方法

注意:所谓5种方法,其本质及底层都是Thread类实现,只是做了相应的封装与设计,以达到其他需求与效果

1、继承Thread类

特点:相对局限,由于java的单继承特性,使得自己的线程类只能继承Thread的方法,没办法再继承其他类。(不推荐使用)

public class ThreadTest {
       private static class Mythread extends Thread {
        @Override
        public void run() {
            for(int i=0; i<10; i++) {
                try {
                    TimeUnit.MICROSECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread" + i );
            }
        }
    }
    public static void main(String[] args) {
        new Mythread().start();
        for(int i=0; i<10; i++) {
            try {
                TimeUnit.MICROSECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("main");
        }
    }
}

2、实现Runnable接口

特点:自己的线程类在实现Runnable接口时还可以继承其他类,相对继承Thread来说更加优雅。

public class ThreadTest {
      static class MyThread implements Runnable {
        @Override
        public void run() {
            System.out.println("MyThread");
        }
    }
    public static void main(String[] args) {
        new Thread(new MyThread()).start();
    }
}

3、Lamda表达式

特点:本质是匿名类、重写run方法

public class ThreadTest {
    public static void main(String[] args) {
        new Thread(()->{
            System.out.println("LambdaThread");
        }).start();
    }
}

4、线程池

特点:* 降低资源消耗,提高响应速度 提高线程的可管理性*,利用已创建的线程执行任务,减少系统创建线程的资源消耗和时间,并对线程进行统一管理、分配、监控、调优

public class ThreadTest {
    static class Task implements Runnable {
            @Override
            public void run() {
System.out.println(Thread.currentThread().getName() + " Task ");
            }
        }
    public static void main(String[] args) {
            ThreadPoolExecutor pool= new ThreadPoolExecutor(2, 4,
                    60, TimeUnit.SECONDS,
                    new ArrayBlockingQueue<Runnable>(4),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.CallerRunsPolicy());
            for (int i = 0; i < 5; i++) {
                pool.execute(new Task());
            }
            pool.shutdown();
        }
}

5、实现Callable类,并结合FutureTask

特点:实现该类的线程类,可有返回值

public class ThreadTest {
   static class Mythread implements Callable{
        @Override
        public String call() throws Exception {
            return "我是可以有返回值的";
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Mythread mythread = new Mythread();
        FutureTask<String> task = new FutureTask<String>(mythread);
        Thread thread = new Thread(task);
        thread.start();
        System.out.println(task.get());
    }
} 	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值