多线程简介与三种创建方法

进程和线程

进程是指一种正在运行的程序,有自己的地址空间。
特点:动态性、并发性、独立性、并发和并行
(并发是指多个cpu同时执行多个任务,并行是一个cpu采用时间分片同时执行多个任务)

线程是进程的一个执行单位,他是程序中一个单一的顺序控制流程,又称为轻量级进程,如果进程同时运行多个线程来同时完成不同的工作,则称为多线程。
特点:轻量级进程、独立调度的基本单位、可并发执行、共享进程资源

线程的三种创建方法

1、继承Thread类实现run方法

public class TestThread {

    public static void main(String[] args) {
        Test test = new Test();
        test.start();
        for (int i = 0; i <500 ; i++) {
            System.out.println("main线程--->"+i);
        }

    }
}

class Test extends  Thread{

    //线程方法
    public void  run(){
        for (int i = 0; i <500 ; i++) {
            System.out.println("Thread线程--->"+i);
        }
    }
}

2、实现Runnable重写run方法

public class TestRunnable {

    public static void main(String[] args) {
        TestRun testRun = new TestRun();
        new Thread(testRun).start();
        for (int i = 0; i <500 ; i++) {
            System.out.println("main线程--->"+i);
        }

    }
}

class TestRun implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i <500 ; i++) {
            System.out.println("Runnable线程--->"+i);
        }
    }
}

3、实现callable接口实现call方法

public class Call {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //创建线程
        ExecutorService service = Executors.newFixedThreadPool(2);
        TestCall tortoies = new TestCall("乌龟", 500);
        TestCall rabbit = new TestCall("兔子", 1000);
        //获取值
        Future<Integer> result1 = service.submit(tortoies);
        Future<Integer> result2 = service.submit(rabbit);

        Thread.sleep(2000);
        tortoies.setFlag(false);
        rabbit.setFlag(false);

        Integer num1 = result1.get();
        Integer num2 = result2.get();
        System.out.println("乌龟跑了--->"+num1+"步");
        System.out.println("兔子跑了--->"+num2+"步");
        //停止服务
        service.shutdownNow();
    }
}

class TestCall implements Callable<Integer>{

    private String name ; //名称
    private long time; //延时时间
    private boolean flag =true;
    private int step =0; //步

    @Override
    public Integer call() throws Exception {
        while (flag){
            Thread.sleep(time);
            step++;
        }
        return step;
    }

    public  TestCall(){

    }
    public TestCall(String name,long time){
        this.name=name;
        this.time=time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值