java——多线程—启动线程

继承Thread启动线程

package com.mycom.继承Thread启动线程;

/**
 *
 * 继承Thread类启动线程的步骤
 * 1.定义自定义线程类并继承Thread
 * 2.重写run方法
 * 3.创建自定义线程对象并调用start()方法
 *
 * 注意:
 *    启动完多线程以后所有的线程会争夺CPU的执行权,哪个线程拿到了执行权哪个线程就会执行代码
 */
public class MyThread extends Thread {
    /**
     * 线程的主体方法,这个方法中应该编写线程需要具体执行的程序代码任务
     * 这个方法不能手动调用,当我们调用当前类对象的start方法以后JVM会自动执行这个run启动一个线程
     */
    public void run() {
       for(;;){
           System.out.println("-----------------------------------------------------");
       }
    }
}

测试:

package com.mycom.继承Thread启动线程;

/**
 * Author: gay
 */
public class RunMain {
    public static void main(String[] args) {
        //创建自定义线程对象
        MyThread t1=new MyThread();
        t1.start();//启动线程
//        t1.run();
        for(;;){
            System.out.println("==================================================");
        }
    }
}

 

实现Runable接口启动线程

package com.mycom.实现Runnabl接口启动线程;

/**
 * 实现Runnable接口启动线程的步骤
 * 1.定义自定义线程类
 * 2.实现Runnable并实现接口中的run方法
 * 3.由于Runnable接口中没有start方法因此必须要借助Thread类启动多线程
 */
public class MyThread implements Runnable {

    public void run() {
        for(;;){
            System.out.println("-------------------------------------------------------");
        }
    }
}

测试:

package com.mycom.实现Runnabl接口启动线程;

/**
 * Author: gay
 */
public class RunMain {
    public static void main(String[] args) {
        MyThread t1=new MyThread();
        Thread thread=new Thread(t1);//根据一个Runnalbe的子类对象创建一个线程对象
        thread.start();//借助Thread类中的start方法启动线程

        //使用匿名内部类启动线程
        new Thread(new Runnable() {
            public void run() {
                for(;;){
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                }

            }
        }).start();
        //使用lambda表达式启动线程
        new Thread(()->{
            for(;;){
                System.out.println("***********************************************************");
            }
        }).start();

        for(;;){
            System.out.println("===========================================");
        }
    }
}

实现Callable接口启动线程

package com.mycom.使用Callable接口启动线程;

import java.util.concurrent.Callable;

/**
 * 实现Callable可以实现多线程的并发计算
 * 在大数据量计算时推荐使用这样的方式来实现
 */
public class MyThread implements Callable<Integer> {

    private int startNum;
    private int stopNum;

    public MyThread(int startNum, int stopNum) {
        this.startNum = startNum;
        this.stopNum = stopNum;
    }

    public MyThread() {
    }

    /*
                并发计算的方法 这个方法将返回计算的结果
            */
    public Integer call() throws Exception {
        int sum=0;
        for(int i=startNum;i<=stopNum;i++){
            sum+=i;
        }
        return sum;
    }
}

测试:

package com.mycom.使用Callable接口启动线程;

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

/**
 * Author: gay
 */
public class RunMain {
    public static void main(String[] args) {


        try {
            MyThread myThread=new MyThread(1,50);
            FutureTask<Integer> ft=new FutureTask<Integer> (myThread);
            Thread thread=new Thread(ft);
            thread.start();
            System.out.println(ft.get());

            MyThread myThread2=new MyThread(51,100);
            FutureTask<Integer> ft2=new FutureTask<Integer> (myThread2);
            Thread thread2=new Thread(ft2);
            thread2.start();
            System.out.println(ft.get()+ft2.get());




        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

 

每个线程都有自己的线程栈

 

posted on 2019-07-12 21:38  荆棘人  阅读(129)  评论(0编辑  收藏  举报

导航