thread_experiment.java

来自「Write a multithreaded Java, Pthreads, or」· Java 代码 · 共 66 行

JAVA
66
字号
/*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 + =
减小字号Ctrl + -
显示快捷键?