美文网首页
java实现多线程的6种方式

java实现多线程的6种方式

作者: 安徒生 | 来源:发表于2018-07-28 13:47 被阅读7次

java实现多线程基本上有5种办法,第一继承Thread类,重写run方法;第二实现继承Runnable接口,重写run方法;第三种是基于内部类的写法,同样重写run方法,实际上和前2种本质一样;第四种,基于带返回值的线程实现方式;第五种,基于线程池的方式;第六种,基于定时任务实现的多线程。

1.基于继承Thread类的实现

/**
* @Author: Cyy
* @Description: 使用继承Thread实现线程的方式
* @Date:Created in 23:33 2018/7/27
*/
public class Thread01 extends Thread {

@Override
 public void run() {
    super.run();
    System.out.println("使用继承Thread实现线程的方式。。。");
    int i=0;
    while (true) {

        System.out.println(Thread.currentThread().getName()+" is Running....");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException  e) {
            e.printStackTrace();
        }
    }
}

public Thread01(String name){
    super(name);
}
public static void main(String[] args) {
    Thread01 thread_1=new Thread01("t1");
    Thread01 thread_2=new Thread01("t2");
    thread_1.start();
    thread_2.start();
    while (true) {
            System.out.println(Thread.currentThread().getName()+" is Running....");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException  e) {
            e.printStackTrace();
        }
    }
 }
}

运行结果:


image.png

2.基于实现Runnable接口的实现

/**
* @Author: Cyy
* @Description:基于实现Runnable接口的实现
* @Date:Created in 23:46 2018/7/27
*/
public class Thread02 implements Runnable {
@Override
public void run() {
    System.out.println("使用实现Runnable实现线程的方式。。。");
    while(true){
            System.out.println(Thread.currentThread().getName()+" is Running....");
        try{
            Thread.sleep(1000);
        }catch (InterruptedException  e){
            e.printStackTrace();
        }
    }
}
public static void main(String[] args) {
    Thread02 thread01=new Thread02();
    Thread02 thread02=new Thread02();
    Thread thread1=new Thread(thread01,"t1");
    thread1.start();
    Thread thread2=new Thread(thread02,"t2");
    thread2.start();
    while (true) {
        System.out.println(Thread.currentThread().getName()+" is Running....");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException  e) {
            e.printStackTrace();
        }
    }
  }
 }

运行结果:


image.png

3.基于内部类的实现

public class Thread00 {

    public static void main(String[] args) {

        //基于子类的实现
        new Thread("t_t"){
            @Override
            public void run() {
                super.run();
                while (true) {
                    System.out.println(Thread.currentThread().getName()+" is Running....");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        //基于接口的实现
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName()+" is Running....");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"t_r"){}.start();

        while (true) {
                System.out.println(Thread.currentThread().getName() + " is Running...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果:


image.png

另外,还有同时实现基于子类和接口的实现的情况:

/**
 * @Author: Cyy
 * @Description: 同时基于子类和内部类
 * @Date:Created in 10:11 2018/7/28
 */
public class Thread03 {

    public static void main(String args[]){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("Runnable is running...");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }){
            @Override
            public void run() {
//                super.run();
                while (true) {
                    System.out.println("sub is running...");
                    try {
                            Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

    }
}

运行结果:


image.png

这里运行结果只有子类线程的打印,原因是虽然实现了Thread类构造办法里Runnable接口的实例,但是子类已经将父类的run方法进行重写了,所以只会执行子类的办法。

4.基于带返回值的线程实现方式

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

/**
 * @Author: Cyy
 * @Description:
 * @Date:Created in 10:35 2018/7/28
 */
public class Thread04 {

    public static void main(String args[]) throws ExecutionException, InterruptedException {
        Callable<Integer> call=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("thread start...");
                Thread.sleep(2000);
                return 520;
            }
        };

        FutureTask<Integer> task=new FutureTask<>(call);
        Thread t=new Thread(task);
        t.start();
        System.out.println("线程的执行结果 "+task.get());

    }
}

运行结果:


image.png

5.基于线程池的方式。

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: Cyy
 * @Description:
 * @Date:Created in 13:02 2018/7/28
 */
public class Thread05 {

    public static void main(String[] args) {

        ExecutorService threadPool= Executors.newFixedThreadPool(5);

        while (true) {
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " is running...");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}

运行结果:


image.png
  • 可以基于ExecutorService、Callable、Future实现有返回结果的多线程。
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author: Cyy
 * @Description:
 * @Date:Created in 13:16 2018/7/28
 */
public class Thread06 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("----程序开始运行-----");

        Date date1=new Date();
        int taskSize=5;

        ExecutorService threadPool= Executors.newFixedThreadPool(taskSize);
        List<Future> list=new ArrayList<Future>();
        for (int i=0;i<taskSize;i++) {
            Callable c=new Callable() {
                @Override
                public Object call() throws Exception {
                    Date dateTmp1 = new Date();
                    Thread.sleep(2000);
                    Date dateTmp2 = new Date();
                    long time = dateTmp2.getTime() - dateTmp1.getTime();
                    return time;
                }
            };
            Future f = threadPool.submit(c);
            list.add(f);
        }
        threadPool.shutdown();
        for (Future f : list) {
            System.out.println(">>>"+f.get().toString());
        }
        Date date2=new Date();
        System.out.println("-----程序结束运行-----,程序运行时间【"+(date2.getTime()-date1.getTime())+"毫秒】");
    }
}

运行结果:


image.png

6.基于定时任务实现的多线程

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author: Cyy
 * @Description:
 * @Date:Created in 16:43 2018/7/28
 */
public class Thread07 {

    private static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    //定时任务
    public static void timeLapse() throws ParseException {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("定时任务开始执行。。。。。");
            }
        },format.parse("2018-07-28 16:51:00"));

    }
    public static void timeLapseBySomeTimes(){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+" is Running...");
            }
        },new Date(),1000);
    }
    public static void main(String[] args) throws ParseException {
//        timeLapse();
        timeLapseBySomeTimes();
        timeLapseBySomeTimes();
        while (true) {
            System.out.println(Thread.currentThread().getName() + " is Running...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果:

image.png

相关文章

  • java多线程

    1.JAVA多线程实现方式 JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用E...

  • Q&A-04 多线程&分布式

    1、Java实现多线程有哪几种方式 Java 多线程实现方式主要有四种: 继承Thread类 实现Runnable...

  • Java基础(六)

    多线程 Java多线程并发 1.1 JAVA 并发知识库 1.2 JAVA 线程实现/创建方式 1.2.1 继承 ...

  • 多线程(最全面试题04)

    Java实现线程有哪几种方式?1、继承Thread类实现多线程2、实现Runnable接口方式实现多线程3、使用E...

  • 多线程经典面试题及答案

    java实现线程有哪几种方式 1.继承Thread类实现多线程2.实现Runnable接口方式实现多线程3.使用线...

  • Java中的多线程实现方式

    Java中的多线程实现方式 在我们的开发的过程中,常常会碰到多线程的问题,对于多线程的实现方式主要有两种:实现Ru...

  • Java多线程知识点

    1.java中实现多线程的几种方式 java中实现多线程的方式主要有两种,第一种是继承Thread类,第二种是实现...

  • Thread,Runnable和Callable

    Java多线程实现方式:Thread,Runnable,Callable多线程是乱序执行Thread===1.Th...

  • Java多线程-实例解析

    Java多线程实例 3种实现方法 Java中的多线程有三种实现方式: 1.继承Thread类,重写run方法。Th...

  • java并发编程(一)

    **java多线程编程(一)** 基本实现方式及同步原理 --- 三种实现方式: 1、继承Thr...

网友评论

      本文标题:java实现多线程的6种方式

      本文链接:https://www.haomeiwen.com/subject/tmonmftx.html