多线程类的案例演示

多线程(thread)

案例一:继承Thread类

/*
   第一步:继承Thread类
   第二步:重写run()方法
   第三步: 创建继承了Thread类的对象,调用stast
*/
public class MyTest extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("进程执行了"+i);
        }
    }

    public static void main(String[] args) {
        MyTest myTest = new MyTest();
        myTest.start();//start()方法来启动线程
        for (int i = 0; i < 10; i++) {
            System.out.println("主函数执行"+i);
        }
    }

案例二:实现runnable

//创建线程方式2:实现runnable接口
//1.实现接口
//2.重写run方法
//3.创建runnbale接口实现
public class MyTest3 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("有人写笔记 "+i);
        }
    }

    public static void main(String[] args) {

        MyTest3 myTest3 = new MyTest3();
        Thread thread = new Thread(myTest3);
        thread.start();
        for (int i = 0; i < 2000; i++) {
            System.out.println("做作业"+i);
        }
    }
}

龟兔赛跑:

public class Rase implements Runnable {
    private static String winner;
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            boolean flag=gameover(i);
            if (flag){
                break;
            }
            System.out.println(Thread.currentThread().getName() + "跑了" + i + "步");
        }
    }
    private boolean gameover(int i) {
        if (winner != null) {
            return true;
        t2.start();
    }
}

静态代理

public class MyTest2 {
    public static void main(String[] args) {
            new wedding(new you()).marry();
    }
}
class you implements marry{

    @Override
    public void marry() {
        System.out.println("我要结婚了");
    }
}
class wedding implements marry{
  private marry you;
  public  wedding(marry you){
      this.you=you;
  }
    @Override
    public void marry() {
        sast();
        you.marry();
        end();
    }

    private void end() {
        System.out.println("婚后");
    }

    private void sast() {
        System.out.println("婚前");
    }
}
interface marry{
    void marry();
}

Lambda表达式

public class Test2 {
    public static void main(String[] args) {
        Ilike like = (a) -> System.out.println("我喜欢Lambda->"+a);
        like.lambda(2);
    }
}
interface Ilike{
    void lambda(int a );
}

休眠倒计时

public class Test {
    public static void main(String[] args) throws InterruptedException {

        while (true){
            Date date = new Date(System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
        }

    }
}

线程的礼让

public class Test2 implements Runnable{
    @Override

    public void run() {
        System.out.println(Thread.currentThread().getName()+"->启动了");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"->停止了");
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        new Thread(test2,"小明").start();
        new Thread(test2,"老师").start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值