多线程实现间隔打印

来源:互联网 发布:jdbc连接数据库 编辑:程序博客网 时间:2024/05/16 11:07
public class TestThread{    public  synchronized void x() {        System.out.println("rgreighreiohgioer");        notify();        try{            wait();        }catch(InterruptedException e){            e.printStackTrace();        }    }    public synchronized void y(){        System.out.println("----------");        notify();        try{            wait();        }catch(InterruptedException e){            e.printStackTrace();        }    }    public static void  main(String[] args){        TestThread t=new TestThread();        new ThreadA(t).start();        new ThreadB(t).start();    }}class ThreadA extends Thread{    private TestThread t;    public ThreadA(TestThread t){        this.t=t;    }    @Override    public void run(){        for(int i=0;i<100;++i){            t.x();        }    }}class ThreadB extends Thread{    private TestThread t;    public ThreadB(TestThread t){        this.t=t;    }    @Override    public void run(){        for(int i=0;i<100;++i){            t.y();        }    }}

//最后还是线程阻塞了23333

原创粉丝点击