2021/8/31 Java多线程1

这篇博客介绍了Java中创建线程的三种方式:继承Thread类、实现Runnable接口以及使用Callable接口。通过实例展示了如何进行线程操作,包括下载网络图片和模拟抢票、龟兔赛跑场景。同时,还提到了静态代理模式和lambda表达式的使用,阐述了它们在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2021/8/31 Java多线程1

线程的创建

方法一:继承Thread类,重写Run()方法,创建Thread对象,调用start()方法启动线程

public class Mythread1 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("我是副线程"+i);
        }
    }
}
  • 例程:用 FileUtilescopyURLtoFile 方法下载网络图片
public class Mythread2 extends Thread { //线程类
    private String url;
    private String fileName;
    public Mythread2(String url,String fileName){
        this.fileName=fileName;
        this.url=url;
    }

    @Override
    public void run() {
        new WebDownloader().downloader(url,fileName);
    }
}

class WebDownloader{ //下载类
    public static void downloader(String url,String file){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(file));
            System.out.println(file+"下载成功");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("出现下载异常"+file);
        }
    }
}

方法二:Runnable()接口,实现Runnable接口,创建Runnable对象,new Thread(Runable对象,“name”).start()

跟继承Thread类没什么区别,主要是因为继承Thread类是智能单继承,继承接口就能实现多继承

//用Runnable接口实现抢票
public class MyThread implements Runnable { //线程类
    private int ticketNum=10;

    @Override
    public void run() {
        while (true) {
            if (ticketNum <= 0) {
                break;
            } else {
                if (Thread.currentThread().getName()=="person1"){
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "买了第" + ticketNum-- + "张票");
            }
        }
    }
   public class Aplication {  //主类【测试类】
    public static void main(String[] args) {
        MyThread p1=new MyThread();
        new Thread(p1,"person1").start();
        new Thread(p1,"person2").start();
        new Thread(p1,"person3").start();
    }
}

用Runnable接口实现龟兔赛跑

//MyThread类
public class MyThread implements Runnable {

    private boolean flag=false;
    @Override
    public void run() {
        for (int i = 0; i <=100 ; i++) {
            if (flag){
                break;
            }
            if(Thread.currentThread().getName()=="兔子"){
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (i == 100) {
                System.out.println(Thread.currentThread().getName() + "赢了");
                flag = true;
            } else {
                System.out.println(Thread.currentThread().getName() + "跑了"+i+"步");
            }
        }
    }
}
//主类【测试类】
public class Aplication {
    public static void main(String[] args) {
        MyThread game=new MyThread();
        new Thread(game,"乌龟").start();
        new Thread(game,"兔子").start();
    }
}

方法三:Callable(了解即可)

优点:有返回值、可以抛出异常

静态代理模式【新知识】

静态代理模式有两个特征:

  1. 需要被代理的真实对象和代理对象都要实现同一个接口
  2. 真实对象只需要完成自己想做的事情,代理对象需要传入真实对象帮他做另外一些事情
//一个很形象的例子
/*
    静态代理案例
    被代理的真实对象:结婚的人
    代理对象:婚庆公司
 */
public class StaticAgentMode {
    public static void main(String[] args) {
        new Agent(new RealMarryer("张三")).weMarry();
    }
}

interface Marry{
    void weMarry();
}

class RealMarryer implements Marry{
    private String name;

    public RealMarryer(String name) {
        this.name = name;
    }

    @Override
    public void weMarry() {
        System.out.println(this.name+"结婚了");
    }
}

class Agent implements Marry{
    private RealMarryer realMarryer;

    public Agent(RealMarryer realMarryer) {
        this.realMarryer = realMarryer;
    }

    @Override
    public void weMarry() {
        beforeMarry();
        this.realMarryer.weMarry();
        after();
    }
    void beforeMarry() {
        System.out.println("婚礼前准备工作");
    }

    void after() {
        System.out.println("婚礼后善后工作");
    }
}

lambda表达式的演化过程

lambda表达式可以使用的前提函数式接口:一个接口如果只有唯一一个抽象方法,就是函数式接口

/*
    总结了lambda表达式一步一步简化的过程
    从外部类-》静态内部类-》局部内部类-》匿名内部类
 */
public class Lambda {
    //2.使用静态内部类
    static class LoveImp2 implements Love{
        @Override
        public void ilove(String name) {
            System.out.println("i love"+name);
        }
    }
    public static void main(String[] args) {
        //3.使用局部内部类
        class LoveImp3 implements Love{
            @Override
            public void ilove(String name) {
                System.out.println("i love"+name);
            }
        }
        Love love=new LoveImp();
        love.ilove("gh");
        love=new LoveImp2();
        love.ilove("gh");
        love=new LoveImp3();
        love.ilove("gh");
        //4.使用匿名内部类
        love=new Love() {
            @Override
            public void ilove(String name) {
                System.out.println("i love"+name);
            }
        };
        love.ilove("gh");
        //5.lambda表达式
        love=(String name)->{
            System.out.println("i love"+name);
        };
        love.ilove("gh");
        //lambda表达式简化
        love=name->System.out.println("i love"+name);
        love.ilove("gh");
    }
}
interface Love {
    void ilove(String name);
}
//1.正常使用外部类
class LoveImp implements Love{
    @Override
    public void ilove(String name) {
        System.out.println("i love"+name);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值