多线程

多线程

继承Thread类

//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程
//注意:线程开启不一定立即执行,由CPU调度执行
public class TestThread1 extends Thread{
    @Override
    public void run() {
        //run方法线程体
        for (int i = 0; i < 20; i++) {
            System.out.println("我在看代码--"+i);
        }
    }

    public static void main(String[] args) {
        //main线程,主线程
        //创建一个线程对象
        TestThread1 testThread1 = new TestThread1();
        //调用start()方法开启线程
        testThread1.start();
        for (int i = 0; i < 200; i++) {
            System.out.println("我在学习多线程--"+i);
        }
    }
}

网图下载

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//练习Thread,实现多线程同步下载图片
public class TestThread2 extends Thread{
    private String url;
    private String name;
    public TestThread2(String url,String name){
        this.url=url;
        this.name=name;
    }
    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:"+name);
    }

    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF","4.jpg");
        TestThread2 t2 = new TestThread2("https://t12.baidu.com/it/u=2557892231,738385457&fm=173&app=49&f=JPEG?w=312&h=208&s=E72BB044CAC0DF3E452A5FC80300E09C","2.jpg");
        TestThread2 t3 = new TestThread2("https://t10.baidu.com/it/u=642235098,1072948198&fm=173&app=49&f=JPEG?w=312&h=208&s=928360A29A81A80DFC03A7D90300109D","3.jpg");
        t1.start();
        t2.start();
        t3.start();
    }
}
class WebDownloader {
    //下载方法
    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常downloader方法出现问题,");
        }
    }
}

实现Runnable接口

//创建线程方式二:(推荐)实现runnable接口,重写run方法,执行线程需要丢入runnbale实现类,调用start方法
public class TestThread3 implements Runnable{
    @Override
    public void run() {
        //run方法线程体
        for (int i = 0; i < 200; i++) {
            System.out.println("我在看代码--"+i);
        }
    }

    public static void main(String[] args) {
        //创建runnable接口的实现类对象
        TestThread3 testThread3 = new TestThread3();
        //创建线程对象,通过线程对象来开启我们的线程,代理
        new Thread(testThread3).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("我再学习多线程--"+i);
        }
    }
}

初始并发问题

package package10;
//多个线程同时操作一个对象
//买火车票的例子
//发现问题:多个线程操作同一个资源的情况下,线程不安全,数据紊乱
public class TestThread4 implements Runnable{
    //票数
    private int ticketNUms = 10;

    @Override
    public void run() {
        while (true){
            if(ticketNUms<=0){
                break;
            }
            //模拟超时
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNUms--+"票");
        }
    }

    public static void main(String[] args) {
        TestThread4 ticket = new TestThread4();
        new Thread(ticket,"小红").start();
        new Thread(ticket,"小明").start();
        new Thread(ticket,"小;李").start();
    }
}


//结果:
//小红-->拿到了第9票
//小明-->拿到了第8票
//小;李-->拿到了第10票
//小;李-->拿到了第7票
//小红-->拿到了第5票
//小明-->拿到了第6票
//小红-->拿到了第4票
//小;李-->拿到了第4票
//小明-->拿到了第3票
//小红-->拿到了第2票
//小;李-->拿到了第0票
//小明-->拿到了第1票

线程停止

//测试stop
//1.建议线程正常停止-->利用次数,不建死循环
//2.建议使用标志位-->设置一个标志位
//3.不要使用stop或者destory等过时或者JDK不建议使用的方法
public class TestStop implements Runnable{
    //1.设置一个标志位
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag){
            System.out.println("run....Thread"+i++);
        }
    }
    //2.设置一个公开的方法停止线程,转换标志位
    public void  stop(){
        this.flag = false;
    }
    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main"+i);
            if (i==900){
                //调用stop方法切换标志位,让线程停止
                testStop.stop();
                System.out.println("线程该停止了");
            }
        }
    }
}

线程休眠

import java.text.SimpleDateFormat;
import java.util.Date;

//模拟倒计时
public class TestSleep {
    public static void main(String[] args) {
        /*try {
            tenDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //打印当前系统时间
        Date startTime = new Date(System.currentTimeMillis());//获取系统当前时间
        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
            startTime = new Date(System.currentTimeMillis());//封信当前时间
        }
    }
    //模拟倒计时
    public static void tenDown() throws InterruptedException {
        int num = 10;
        while (true){
            Thread.sleep(1000);
            System.out.println(num--);
            if(num<=0){
                break;
            }
        }
    }
}

线程礼让

//测试礼让线程
//礼让不一定成功,看CPU心情
public class TestYield {
    public static void main(String[] args) {
        MyYield myYield = new MyYield();
        new Thread(myYield,"a").start();
        new Thread(myYield,"b").start();
    }
}
class MyYield implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始执行");
        Thread.yield();//礼让
        System.out.println(Thread.currentThread().getName()+"线程开始停止");
    }
}

线程强制执行

public class TestJoin implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("线程vip来了"+i);
        }
    }

    public static void main(String[] args) {
        //启动我们的线程
        TestJoin testJoin = new TestJoin();
        Thread thread = new Thread(testJoin);
        thread.start();
        //主线程
        for (int i = 0; i < 500; i++) {
            if (i==200){
                try {
                    thread.join();//插队
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("main"+i);
        }
    }
}

线程的优先级

//测试线程的优先级
public class TestPrioritu {
    public static void main(String[] args) {
        //主线程默认优先级
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
        MyPrority myPrority = new MyPrority();
        Thread t1 = new Thread(myPrority);
        Thread t2 = new Thread(myPrority);
        Thread t3 = new Thread(myPrority);
        Thread t4 = new Thread(myPrority);
        Thread t5 = new Thread(myPrority);
        Thread t6 = new Thread(myPrority);
        //先设置优先级,再启动
        t1.start();
        t2.setPriority(1);//设置优先级
        t2.start();
        t3.setPriority(4);//设置优先级
        t3.start();
        t4.setPriority(Thread.MAX_PRIORITY);//设置优先级
        t4.start();
        t5.setPriority(8);//设置优先级
        t5.start();
        t6.setPriority(7);//设置优先级
        t6.start();
    }
}
class MyPrority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
    }
}


//结果
main-->5
Thread-0-->5
Thread-3-->10
Thread-4-->8
Thread-2-->4
Thread-5-->7
Thread-1-->1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李拜天不上班.

感谢支持,承蒙厚爱!

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

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

打赏作者

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

抵扣说明:

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

余额充值