线程创建的三种方式

 

 

创建线程方式一:重写run()方法,调用start开启线程

 

package testthread;

/**
 * @author dell
 */
//创建线程方式一:重写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 thread1 = new TestThread1();
        //调用start开启线程
        thread1.start();

        for (int i = 0; i < 20; i++) {
            System.out.println("我在学习多线程"+i);
        }
    }
}

 

网图下载

package testthread;

import org.apache.commons.io.FileUtils;

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

/**
 * 实现多线程同步下载图片
 */
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() {
        WebDownload webDownload = new WebDownload();
        webDownload.download(url,name);
        System.out.println("下载了文件名"+name);

    }
    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://img2020.cnblogs.com/blog/2592859/202110/2592859-20211029122522433-1798243557.png","t1.png");
        TestThread2 t2 = new TestThread2("https://img2020.cnblogs.com/blog/2592859/202110/2592859-20211029122522433-1798243557.png","t2.png");
        TestThread2 t3 = new TestThread2("https://img2020.cnblogs.com/blog/2592859/202110/2592859-20211029122522433-1798243557.png","t3.png");
        t1.start();
    }
}

class WebDownload{
    public void download(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,download方法出现问题");
        }
    }

}

 

 

 

创建线程方式二:实现Runnable接口,重写run方法,执行线程需要丢入Runnable接口实现类,调用start方法

package testthread;

/**
 * 创建线程方式二:实现Runnable接口,重写run方法,执行线程需要丢入Runnable接口实现类,调用start方法
 */
public class TestThread3 implements Runnable {
    public void run() {
        //run方法线程体
        for (int i = 0; i < 20; 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 < 20; i++) {
            System.out.println("我在学习多线程"+i);
        }
    }
}

  

初始多并发问题

package testthread;
//模拟抢票
public class TestThread5 implements Runnable {

    private int ticketNums = 10;
    public void run() {
        while (true){
            if (ticketNums<=0){
                break;
            }
            //模拟延时
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"票");
        }
    }

    public static void main(String[] args) {
        TestThread5 testThread5 = new TestThread5();
        new Thread(testThread5,"小明").start();
        new Thread(testThread5,"小红").start();
        new Thread(testThread5,"小花").start();
    }
}

  

龟兔赛跑

package testthread;
//模拟龟兔赛跑
public class TestThread6 implements Runnable {
    //胜利者
    private static String winner;
    public void run() {
        for (int i = 0; i <= 100; i++) {
            //模拟兔子休息
            if (Thread.currentThread().getName().equals("兔子") && i/10==0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //判断比赛是否结束
            boolean flag = gameOver(i);
            //如果比赛结束,就停止程序
            if (flag){
                break;
            }
            System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"步");
        }
    }
    //判断是否完成比赛
    public boolean gameOver(int steps){
        if (winner != null){
            return true;
        }{
            if (steps>=100){
                winner=Thread.currentThread().getName();
                System.out.println("winner is"+winner);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        TestThread6 testThread6 = new TestThread6();
        new Thread(testThread6,"乌龟").start();
        new Thread(testThread6,"兔子").start();
    }
}

  

  

  

posted on 2021-10-29 16:37  KuangPlus  阅读(50)  评论(0)    收藏  举报