力扣 Q1114 多线程 按序打印(针对今天学的)内含线程创建方式

今天做一道关于线程的题目:力扣链接:https://leetcode.cn/problems/print-in-order/
在这里插入图片描述
要点:掌握线程的创建方法 sleep() 和notifyall()的定义及使用
运行成功截图
在这里插入图片描述
具体代码实现

package com.zjy.leetcode;
//
public class Anxupaixu {
    public static void main(String[] args) {
        Foo foo=new Foo();
        Thread t1=new Thread(()-> {
            try {
                foo.first(()->System.out.println("first"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t2=new Thread(()-> {
            try {
                foo.second(()->System.out.println("second"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t3=new Thread(()-> {
            try {
                foo.third(()->System.out.println("third"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
        t3.start();

    }
}
class Foo {
    private int count =1;

   public Foo(){

   }

    public synchronized void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        count++;
        notifyAll();
    }

    public synchronized void second(Runnable printSecond) throws InterruptedException {
        while(this.count!=2){
            wait();
        }
        this.count++;
        notifyAll();
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
    }

    public synchronized void third(Runnable printThird) throws InterruptedException {
        while(this.count !=3){
            wait();
        }

        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }
}

其中创建线程的方法是lamda 匿名内部类
在这里插入图片描述
thread 单继承实现 改写实例

public class Anxupaixu {
    public static void main(String[] args) {
        Foo foo=new Foo();
        SomeThread1 t1=new  SomeThread1();
        t1.start();
        SomeThread2 t2=new  SomeThread2();
        t2.start();
        SomeThread3 t3=new  SomeThread3();
        t3.start();

    }
}

class SomeThread1 extends Thread{
    Foo foo=new Foo();
    public void run(){
        try {
            foo.first(()->System.out.println("first"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class SomeThread2 extends Thread{
    Foo foo=new Foo();
    public void run(){
        try {
            foo.first(()->System.out.println("2"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class SomeThread3 extends Thread{
    Foo foo=new Foo();
    public void run(){
        try {
            foo.first(()->System.out.println("3"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述
照样通过

线程创建的方法二:runnable

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值