您的位置:首页 > 其它

实现T1线程执行后再执行T2线程,之后再执行T3线程

2017-05-14 20:40 375 查看
public class TestJoin

{

    public static void main(String[] args)
    {

        Store sotre = new Store();

        T1 t1= new T1(sotre, "线程1");

        Thread t2 = new Thread(sotre, "线程2");

        Thread t3 = new Thread(sotre, "线程3");

        

        try

        {

            //t1先启动

            t1.start();

            t1.join();

            //t2

            t2.start();

            t2.join();

            //t3

            t3.start();

         

        }

        catch (InterruptedException e)

        {

            e.printStackTrace();

        }

    }

}

/**

 *

 *

 * <p>

 * Title:共享资源

 * </p>

 * <p>

 * Description:it.cn.itcast.Store.java

 * </p>

 * <p>

 * Copyright: 2017-5-14下午6:09:27

 * </p>

 * <p>

 * Company:

 * </p>

 *

 * @author King

 */

public class Store{

    

    private int count;

    private int size;

    

    public Store() {

        this.count = 0;

        this.size = 10;

        

    }

    public Store(int count, int size) {

        super();

        this.count = count;

        this.size = size;

    }

    public int getCount() {

        return count;

    }

    public void setCount(int count) {

        this.count = count;

    }

    public int getSize() {

        return size;

    }

    public void setSize(int size) {

        this.size = size;

    }

    public void addStore() {

        count++;    

        System.out.println("thread- add-->"+Thread.currentThread().getName());

    }

    public void removeStore() {

        count--;

        System.out.println("thread---remove-->"+Thread.currentThread().getName());

    }

    

    public boolean isFull() {

            

        

        return this.count >= size;

    }

    public boolean isEmpty() {

        

        

        return this.count <= 0;

    }

}

class T1 extends Thread{

    

    @Override

    public void run()

    {

        for (int i = 0; i < 5; i++)

        {

            System.out.println(Thread.currentThread().getName()+": "+i);

            try

            {

                Thread.sleep(100);

            }

            catch (InterruptedException e)

            {

                e.printStackTrace();

            }

        }

    }

}

class T2 extends Thread{

    

    @Override

    public void run()

    {

        for (int j = 5; j < 10; j++)

        {

            System.out.println(Thread.currentThread().getName()+": "+j);

            try

            {

                Thread.sleep(100);

            }

            catch (InterruptedException e)

            {

                e.printStackTrace();

            }

        }

    }

}

class T3 extends Thread{

    @Override

    public void run()

    {

        for (int i = 10; i < 15; i++)

        {

            System.out.println(Thread.currentThread().getName()+": "+i);

            try

            {

                Thread.sleep(100);

            }

            catch (InterruptedException e)

            {

                e.printStackTrace();

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐