求大神帮忙:启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 而后是线程2打印6,7,8,9,10, 而后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,...

package com.unit10.HomeWork;public class HomeWork003 {    /**     * 建立三个线程进行分段叠加     * 每一个线程叠加四次     *     */    public static void main(String[] args) {        new Thread(new Add(1)).start();        new Thread(new Add(2)).start();        new Thread(new Add(3)).start();    }}class Add implements Runnable{    //定义一个线程ID    private int threadID;    //须要进行叠加的数字    private static int printNum = 0;    //构造方法获取thread的ID    public Add(int threadID){        this.threadID = threadID;    }    @Override    public void run() {        //须要叠加的数字小于75继续叠加        while (printNum  < 75){            //Add.class表示  Add 对象 = new Add();            //Add.class表示Add类的一个不肯定对象            synchronized (Add.class){                System.out.println("当前的线程是:"+"---->"+threadID+"线程");                int index = (printNum/5%3+1);                System.out.println("当前的数字是:"+"---->"+printNum);                System.out.println("当前的指针是:"+"---->"+index);                //线程ID和指针相同时能够进入叠加for循环                if(index == threadID){                    for ( int i = 0 ; i < 5 ; i++){                        System.out.println("线程"+threadID+":"+(++printNum));                    }                    //执行完毕,唤醒其余线程                    Add.class.notify();                }else {                    System.out.println("我去换个线程!");                    try {                        Add.class.wait();                        //该线程不和条件,进行等待其余线程                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }    }}
相关文章
相关标签/搜索