developS

使用线程实现ACB的顺序输出

在java中可以使用join方法来实现,join会阻塞当前方法,调用的当前方法执行结束后,才会继续往下执行!

public class Foo {

    public Foo() {
    }

    public void A(){
        System.out.println("A");
    }

    public void B(){
        System.out.println("B");
    }

    public void C(){
        System.out.println("C");
    }
}
public class SortContent {


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


        Thread ta= new ThreadA();
        Thread tb= new ThreadB();
        Thread tc= new ThreadC();

//使用匿名内部类建立多线程
//        Thread ta= new Thread(()->new Foo().A());
// Thread tB= new Thread(()->new Foo().B());
// Thread tC= new Thread(()->new Foo().C());
ta.start(); ta.join(); tc.start(); tc.join(); tb.start(); } }
//继承thread类实现
class ThreadA extends Thread{ @Override public void run(){ new Foo().A(); } } class ThreadB extends Thread{ @Override public void run(){ new Foo().B(); } } class ThreadC extends Thread{ @Override public void run(){ new Foo().C(); } }

 

posted on 2023-08-14 19:01  四十四次日落95  阅读(18)  评论(0)    收藏  举报