多线程

多线程

Thread

如何使用

说明:
  1. Thread 是一个类,如果要使用它就需要继承这个Thread类
  2. 在继承Thread类,需要重新run方法run方法线程体
  3. 创建一个线程对象,在调用start方法开启线程
继承Thread类,以及重写run方法
public class Thread _01 extends Thread{
    public void run(){
        
    }
    
}
在main 方法里创建一个线程对象
public static void main(String [] args){
    Thread_01 thread_01 = new Thread_01();
    
}
调用start方法
thread_01.start();

线程的作用

package com.Thread;
/*创建线程,要继承Thread类 重写run方法*/
public class Thread_01 extends Thread{
    @Override
    //重写方法
    public void run() {
        //run方法线程体
        for (int i = 0; i < 20; i++) {
            System.out.println("我在玩hhh"+i);
        }
    }
    public static void main(String[] args) {
        //创建一个线程对象 调用start方法
        Thread_01 thread_01 = new Thread_01();
        //start() 开启线程
        thread_01.start();

        for (int i = 0; i < 20; i++) {
            System.out.println("我在看"+i);

        }
    }
}

在run方法线程体中,放一个for语句把我在玩hhh循环20次

在main方法也放一个for语句把我在看循环20次

输出结果:

在这里插入图片描述

感觉线程没啥作用,在来把循环的次数加大,还是先执行我在看,然后执行我在玩hhh

 for (int i = 0; i < 2000; i++) {
            System.out.println("我在玩hhh"+i);
        }
 for (int i = 0; i < 2000; i++) {
            System.out.println("我在看"+i);
        }

在看结果:

在这里插入图片描述

他们同时进行执行的,不是先执行main方法,在执行run方法

总结Thread

run方法是跟main方法一起执行的

下载照片的玩法,以及使用commons-io-2.8.0包(下载包)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值