java:多线程

线程(Thread)是一个程序内部的一条执行流程。

程序中如果只有一条执行流程,那这个程序就是单线程的程序。

什么是多线程?

多线程是指从软硬件实现的多条执行流程的技术(多条线程由cpu负责调度执行)。

如何在程序中创建出多条线程?

多线程的创建方式一:继承Thread类继承。

package Theaddemo1;

public class test {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        for (int i = 0; i < 100; i++) {
            System.out.println("主线程:" + i);
        }
    }
}

class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("子线程:" + i);
        }
    }
}

多线程创建方法二:实现Runnable接口

package Theaddemo1;

public class demo2 {
  public static void main(String[] args) {
      Runnable task = new MyRunnable();
      Thread thread = new Thread(task);
      thread.start();

      for (int i = 0; i < 100; i++) {
          System.out.println("主线程:" + i);
      }
  }
   static class MyRunnable implements Runnable
    {
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println("子线程:" + i);
            }
        }
    }
}

优点:人物类只是实现接口,可以继承其他类、实现其他接口,扩展性强。

缺点:需要多一个Runnable对象。

多线程创建方法三:实现callable接口

这种最大优点:可以返回线程执行完毕的结果。

package Theaddemo1;

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

public class demo3 {
    public static void main(String[] args) {
        MyCallable task = new MyCallable(100);
        MyCallable task2 = new MyCallable(50);
        FutureTask<String> task3 = new FutureTask<>(task2);
        FutureTask<String> task1 = new FutureTask<>(task);
        Thread thread = new Thread(task1);
        Thread thread2 = new Thread(task3);
        thread.start();
        thread2.start();
        try {
            System.out.println(task1.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            System.out.println(task3.get());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    static class MyCallable implements Callable<String> {
        private int n;

        public MyCallable(int n) {
            this.n = n;
        }

        @Override
        public String call() throws Exception {
            int sum=0;
            for (int i = 1; i <= n; i++) {
                System.out.println(":" + i);
                sum = n + i;
            }
            return "1到"+ n+"="+sum;
        }

        }
        }

Thread 的常用方法

Thread提供的常用方法说明
public void run()线程的任务方法
public void start()启动线程
public String getName()获取当前线程的名称,线程名称默认是Thread-索引
public void setName(String name)为线程设置名称
public static Thread currentThread()获取当前执行的线程对象
public static void sleep(long time)让当前执行的线程休眠多少毫秒后,在继续执行。
public final void join()让调用当前这个方法的线程先执行完!

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值