编程语言
首页 > 编程语言> > java多线程概念及实现方式

java多线程概念及实现方式

作者:互联网

并发和并行

并发:在同一时刻,有多个指令在单个CPU上交替执行.

并行:在同一时刻,有多个指令在多个CPU上同时执行.

进程和线程

进程:正在运行的软件.

线程:进程中的单个顺序控制流,是一条执行路径.

 

多线程的实现方式

多线程的实现方案

方式1:继承Thread类

  1. 定义一个类如MyThread继承Thread类
  2. 在MyThread类中重写run()方法
  3. 创建MyThread类的对象
  4. 启动线程
public class MyThread extends Thread {
    @Override
    public void run() {
//        run方法是执行的内容
        for (int i = 0; i < 100; i++) {
            System.out.println("线程开启了"+i);
        }
    }
}
public class MyThreadTest {
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();
        t1.start();
        t2.start();
//        可以发现两个线程是交替执行的
    }
}

两个小问题

为什么要重写run方法?

方式2:实现Runnable

  1. 定义一个类MyRunnable实现Runnable接口
  2. 在MyRunnable类中重写run()方法
  3. 创建MyRunnable类的对象
  4. 创建Thread类的对象,把MyRunnable对象作为构造方法的参数
  5. 启动线程
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("第二种方式"+i);
        }
    }
}
public class MyRunnableTest {
    public static void main(String[] args) {
//        创建一个参数对象
        MyRunnable m1 = new MyRunnable();
        MyRunnable m2 = new MyRunnable();
//        创建一个线程对象,并把参数传给这个线程
        Thread t1 = new Thread(m1);
        Thread t2 = new Thread(m2);
//        开启线程
        t1.start();
        t2.start();
    }
}

方式3:Callable和Future

区别:Callable接口有返回值,并且可以抛出异常;

          Future 主要作用:获取任务执行结果,中断任务等 

  1. 定义一个类MyCallable实现Callable接口
  2. 在MyCallable类中重写call()方法·创建MyCallable类的对象
  3. 创建Future的实现类FutureTask对象,把MyCallable对象作为构造方法的参数
  4. 创建Thread类的对象,把FutureTask对象作为构造方法的参数
  5. 启动线程
  6. 再调用get方法,就可以获取线看程结束之后的结果。
  7. 值得注意的是:get方法要在start开启之后调用.
public class MyCallable implements Callable<String>{
    @Override
    public String call() throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.println("线程执行中"+i);
        }
        return "线程执行完成";
    }
}

  

public class MyCallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable m1 = new MyCallable();
//        可以获取线程执行后的结果,也可以作为参数传递诶Thread
        FutureTask<String> f1 = new FutureTask(m1);
        Thread t1 = new Thread(f1);
//        开启线程
        t1.start();
//        返回值
        String o = f1.get();
        System.out.println(o);
    }
}

  

标签:MyRunnable,java,Thread,概念,线程,new,run,多线程,public
来源: https://www.cnblogs.com/ljstudy/p/14453867.html