线程的理解与创建

线程定义

线程是‌操作系统能够进行运算调度的最小单位,它是‌进程中可独立执行的子任务。 线程是操作系统中用于并发执行任务的基本单元,每个进程可以包含一个或多个线程。这些线程在进程中并发执行,允许同时处理多个任务,从而提高系统的整体性能和响应速度。

线程与进程的区别

  • 进程

进程是系统进行资源分配和调度的基本单位,每个进程拥有独立的‌内存空间和资源。进程的创建、‌销毁需要相对较多的系统资源。

  • 线程

线程是进程中的一个执行单元,它共享进程的内存空间和资源。线程的创建、销毁相对于进程来说更加轻量级,切换开销小,适合执行同一进程内的并发任务。

线程的创建与销毁

  • 创建

线程的创建通常通过系统调用或库函数实现,如pthread_create(在POSIX系统中)或‌Java中的Thread类构造函数。

  • 销毁

线程可以通过调用特定的函数(如pthread_exit)或被外部信号中断来结束其执行。

线程的创建方式

线程的创建方式理论上只有一种

那就是创建Thread实例

创建线程的实现方式有4种
1、继承Thread
public class SubThread extends Thread {
  //继承Thread,并重写run()方法
       public void run() {
           for (int i = 0; i < 10000; i++) {
               System.out.println("子线程" + i + " ");
           }
       }
}

// 主线程main
public class MultiThread {
       public static void main(String[] args) {
            //创建并启动子线程
           SubThread thd = new SubThread();
           thd.start();
           
           //主线程继续同时向下执行
           for (int i = 0; i < 10000; i++) {
               System.out.println("主线程" + i + " ");
           }
       }
}
2、实现Runnable接口
public class SubThread implements Runnable {
  //定义一个类,实现Runnable接口
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println("子线程" + i + " ");
        }
    }
}

// 主线程 main
public class MultiThread {
    public static void main(String[] args) {
        //创建并启动子线程
        Thread t = new Thread(new SubThread());
        t.start(); 

        //主线程继续同时向下执行
        for (int i = 0; i < 10000; i++) {
            System.out.println("主线程" + i + " ");
        }
    }
}
3、实现Callable接口
public class SubThread implements Callable<Integer>{
    private int begin,end;
    public SubThread(int begin,int end){
        this.begin = begin;
        this.end = end;
    }
    @Override
    public Integer call() throws Exception {
        int result = 0;
        for(int i=begin;i<=end;i++){
            result+=i;
        }
        return result;
      }
}

// 主线程 main
public class MultiThread {
    public static void main(String[] args) {
        
        // 子线程封装为FutureTask对象,计算1-100的累加和
        SubThread subThread1 = new SubThread(1,100);
        FutureTask<Integer> task1 = new FutureTask<>(subThread1);
        
        // 子线程封装为FutureTask对象,计算101-200的累加和
        SubThread subThread2 = new SubThread(101,200);
        FutureTask<Integer> task2 = new FutureTask<>(subThread2);
        
        // 分别启动两个子线程
        new Thread(task1).start();
        new Thread(task2).start();
        
        // 分别获取两个子线程的计算结果
        int sum1 = task1.get();
        int sum2 = task2.get();
        
        // 汇总计算结果
        int total = sum1 + sum2;
    }
}
4、线程池
ExecutorService threadPool = Executors.newFixedThreadPool(10);
while (true) {
    // 提交多个执行任务至线程池,并执行
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             System.out.println("当前运行的线程名为: " + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
}
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿究院-Cu-Sn合金

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值