2018.1.22

2018.1.22

class producer implements Runnable{
    int ms,n;
    producer(int ms,int n){
        this.ms = ms;
        this.n=n;
    }
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        try {
            while (count<stop) {
                produce();
                Thread.sleep(ms);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    void produce() throws Exception {
        synchronized (queue){
            // TO DO
            for(int i=0;i<n;i++) {
                if(count >= max)
                    break;
                queue.add(count);
                textView.setText(textView.getText().toString()+Thread.currentThread().getName()+" 生产了:"+count+"还剩:"+queue.size()+'\n');
                count++;
            }
        }
    }
}

在这里插入图片描述

原创文章 327 获赞 212 访问量 40万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/105865084