zoukankan      html  css  js  c++  java
  • 【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;
            }
        }
    }
  • 相关阅读:
    关于BlockingQueue
    关于java的线程
    mongodb的锁和高并发
    innodb的锁和高并发
    mysql的事务隔离级别及其使用场景
    mongodb分页
    ReentrantLock和Synchronized
    spring boot MVC
    svn 入门
    多线程的返回值等问题
  • 原文地址:https://www.cnblogs.com/LPworld/p/10724103.html
Copyright © 2011-2022 走看看