【Java例题】7.3 线程题3-素数线程


3.素数线程。
设计一个线程子类,依次随机产生10000个随机整数(100~999);
再设计另一个线程子类,依次对每一个随机整数判断是不是素数,是则显示;
然后编写主类,在主函数中定义这两个线程类的线程对象,并启动它们。

package chapter7;

public class demo3 {
    static int a[]=new int[10000];
    public static void main(String[] args) {
        Runnbale1 r1=new Runnbale1();
        Runnbale2 r2=new Runnbale2();
        new Thread(r1).start();
        new Thread(r2).start();
    }

    
    private static class Runnbale1 implements Runnable{
        public void run() {
            for(int i=0;i<10000;i++) {
                a[i]=(int)(100+Math.random()*(999-100));
            }
        }
    }
    private static class Runnbale2 implements Runnable{
        public void run() {
            for(int i=0;i<10000;i++) {
                if(issushu(a[i])) {
                    System.out.println(a[i]);
                }
            }
        }
        
        boolean issushu(int num) {
            int i;
            for(i=2;i<Math.pow(num, 0.5);i++) {
                if(num%i==0) {
                    return false;
                }
            }
            return true;
        }
    }
}

 

转载于:https://www.cnblogs.com/LPworld/p/10724103.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值