Java多线程

本篇介绍Java多线程。能够并发执行任务。但要注意的是线程创建太多,会导致CPU 花费在上下文的切换的时间将多于执行程序的时间。程序执行效率反而降低。

创建线程方式有以下3种

1.通过实现Runnable接口创建线程;

2.通过继承Thread类创建线程;

3.通过 Callable 和 Future 创建线程;

package com.example.javatest;

/**
 * Author:W
 * 通过实现Runnable接口来实现
 */
public class Thread1 implements Runnable {
    private  Thread  thread;
    private  String  threadName;

    public Thread1(String threadName)
    {
        this.threadName = threadName;
        System.out.println("线程"+threadName+" :创建成功!");
    }

    @Override
    public void run() {
        System.out.println("线程"+threadName+" :正在运行。。。");
        try
        {
            for (int i=0;i<3;i++)
            {
                System.out.println("线程"+threadName+" 正在运行。。。打印:"+ i);
                Thread.sleep(50);
            }
        }catch (InterruptedException e)
        {
            System.out.println("线程" +  threadName + " 异常中断");
        }

        System.out.println("线程" +  threadName + " :结束退出");
    }

    //线程开始
    public void start()
    {
        System.out.println("线程"+threadName+" :开始");
        if (thread == null)
        {
            thread = new Thread(this,threadName);
            thread.start();
        }
    }
}
package com.example.javatest;

/**
 * Author:W
 * 通过继承Thread类创建线程
 */
public class Thread2 extends Thread {
    public  Thread thread;
    public  String threadName;

    public  Thread2(String threadName)
    {
        this.threadName = threadName;
        System.out.println("线程"+threadName+" :创建成功!");
    }

    //线程开始执行
    public  void  run()
    {
        System.out.println("线程"+threadName+" :正在运行。。。");
        try
        {
            for (int i=0;i<2;i++)
            {
                System.out.println("线程"+threadName+" 正在运行。。。打印:"+ i);
                Thread.sleep(50);
            }
        }catch (InterruptedException e)
        {
            System.out.println("线程" +  threadName + " 异常中断");
        }

        System.out.println("线程" +  threadName + " :结束退出");
    }

    //线程开始
    public void start()
    {
        System.out.println("线程"+threadName+" :开始");
        if (thread == null)
        {
            thread = new Thread(this,threadName);
            thread.start();
        }
    }
}
package com.example.javatest;

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

/**
 * Author:W
 * 通过 Callable 和 Future 创建线程
 */
public class Thread3 implements Callable<Integer> {
    public  Thread thread;
    public  String threadName;

    public  Thread3(String threadName)
    {
        this.threadName = threadName;
        System.out.println("线程"+threadName+" :创建成功!");
    }

    @Override
    public Integer call() throws Exception {
        System.out.println("线程"+threadName+" :正在运行。。。");
        int a = 0;
        for (int i=0;i<5;i++)
        {
            a = i;
            System.out.println("线程"+threadName+" 正在执行。。。打印:"+i);
        }

        System.out.println("线程" +  threadName + " :结束退出");
        return a;
    }

    public void start()
    {
        FutureTask<Integer>  futureTask = new FutureTask<Integer>(this);
        if (thread == null)
        {
            thread = new Thread(futureTask,threadName);
            thread.start();
        }

        try
        {
            System.out.println("线程"+threadName+"执行任务完成返回值:"+ futureTask.get());
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }catch (ExecutionException e)
        {
            e.printStackTrace();
        }
    }
}

线程测试脚本

package com.example.javatest;//包名定义

/**
 * Author:W
 * 多线程创建使用:并发执行任务。注意:线程创建太多,会导致CPU 花费在上下文
 * 的切换的时间将多于执行程序的时间。程序执行效率反而降低。
 * 1.通过实现Runnable接口创建线程
 * 2.通过继承Thread类创建线程
 * 3.通过 Callable 和 Future 创建线程
 */
public class MainTest{

    public static void main(String[] args)
    {
          System.out.println("===1.通过实现Runnable接口创建线程===");
          Thread1  thread1 = new Thread1("Runnable");
          thread1.start();

          System.out.println("===2.通过继承Thread类创建线程===");
          Thread2  thread2 = new Thread2("Thread");
          thread2.start();

          System.out.println("===3.通过 Callable 和 Future 创建线程===");
          Thread3  thread3 = new Thread3("Callable");
          thread3.start();
    }

}

运行结果如下:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Data菌

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

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

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

打赏作者

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

抵扣说明:

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

余额充值