3.30。

/**
 * 编写一个有两个线程的程序,第一个线程用来计算2~100000之间的素数的个
 * 数,第二个线程用来计算100000~200000之间的素数的个数,最后输出结果。
 */

public class Zuoye {
    /**
     * 编写一个有两个线程的程序,第一个线程用来计算2~100000之间的素数的个
     * 数,第二个线程用来计算100000~200000之间的素数的个数,最后输出结果。
     */
    public class Zuoye01 extends  Thread{
    private  int min;
    private  int max;
    private int index=0;
    private boolean flag=true;

    public Zuoye01(String name, int min, int max) {
        super(name);
        this.min = min;
        this.max = max;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public  void run() {
        for (int i = min; i <max ; i++) {
            flag=true;
            for (int j = 2; j < i; j++) {
                if (i%j==0)
                {
                    flag=false;
                    break;
                }
            }
            if (flag)
            {
                index++;
            }
        }

    }

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }
}
    
    public static void main(String[] args) throws InterruptedException {

        Zuoye01 t1=new Zuoye01("hah",2,100000);
        Zuoye01 t2=new Zuoye01("t3",100000,200000);
t1.start();
t2.start();
t1.join();
t2.join();
        System.out.println(t1.getIndex());
        System.out.println(t2.getName()+":"+t2.getIndex());
    }
}
    public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(){
    @Override
    public void run() {
        int index=0;
        boolean flag=true;
        for (int i = 2; i <100000 ; i++) {
            for (int j = 2; j <i ; j++) {
                flag=true;
                if (i%j==0)
                {
                    flag=false;
                    break;
                }
            }if (flag)
            {
                index++;
            }
        }
        System.out.println("2-100000:"+index);
    }
};
Thread t2=new Thread(){
    @Override
    public void run() {
        int index=0;
        boolean flag=true;
        for (int i = 100000; i <200000 ; i++) {
            for (int j = 2; j <i ; j++) {
                 flag=true;
                if (i%j==0)
                {
                    flag=false;
                    break;
                }
            }if (flag)
            {
                index++;
            }
        }
        System.out.println("100000-200000:"+index);
    }
};
t1.start();
t2.start();
    }
}
 /**
 * 模拟多线程并发问题,并解决(方式越多越好)
 * 模拟两个人(两个线程),同时在桌子上拿豆子,考虑并发出错情况
 */ 


public static void main(String[] args) {
        Douzi d1 = new Douzi(100);
        Thread t1 = new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        d1.getDou();
                        System.out.println(Thread.currentThread().getName()+":"+
                                d1.getNum());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        d1.getDou();
                        System.out.println(Thread.currentThread().getName()+":"+
                                d1.getNum());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        t1.start();
        t2.start();
    }

}
class Douzi{
    private int num;

    public Douzi(int num) {
        this.num = num;
    }

    public void getDou() throws Exception {
        if(num==0){
            throw new Exception("没豆子了");
        }
        else num--;
    }
    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值