⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread_experiment.java

📁 Write a multithreaded Java, Pthreads, or WIN32 program that outputs prime numbers. This program shou
💻 JAVA
字号:
/*ThreadTester is a class in which the running of the thread executes.
 */
import java.io.*;

public class ThreadTester
{
    public static void main(String[] args)
    {
        if(args.length <= 0)
        {
            System.err.println("Usage: java ThreadTester <integer value>");
            return;
        }
       
        else
        {
            Prime thrd = new Prime(Integer.parseInt(args[0]));
            thrd.start();
           
        }
    }
}

 

/* Prime is a class in which the code for the thread exits. It prints out all prime number no greater than the given one.
 */
class Prime extends Thread
{
    private int upper;
   
    public Prime(int n)
    {
        upper = n;
    }
   
    public void run()
    {   
        int i,j = 0;
       
        if( upper < 0)
        {
            System.err.println("The number must be greater than 0!");
            return;
        }
       
        else if(upper == 0 || upper == 1)
        {
            System.out.println("No prime numbers ... ");
        }
       
        else
        {
            for(i=2; i<=upper; i++)
            {
                for(j=2; j<i/2+1; j++)
                {
                    if(i%j == 0)
                        break;
                }
                if(j >= i/2+1)
                    System.out.println(i);
            }
        }
    }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -