简体   繁体   English

我想使用两个线程 evenCount 和 oddCount 找到偶数和奇数

[英]I want to find even and odd number using two thread evenCount and oddCount

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author user
 */



class evenCount extends Thread {
    public void run(){
        for(int i = 2; i >=20; i++)
        {
            try 
            {
                Thread.sleep(500);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
            System.out.println(i);
        }
    }
}

class oddCount extends Thread {
    
    public void run(){
    for(int i = 21; i>=31; i++)
    {
        try
        {
            Thread.sleep(700);
        }
        
        catch(Exception e)
        {
            System.out.println(e);
        }
        
        System.out.println(i);
    }
    }
}

class mainThread{

    public static void main(String args[]){
        evenCount even = new evenCount();
        oddCount odd  = new oddCount();
        
        even.run();
        odd.run();
    }
}

```````````````````Description```````````````````````` ````````````````````描述````````````````````````

I want to find even and odd numbers but the output show me like this:我想找到偶数和奇数,但 output 像这样向我显示:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Thread.线程“主”java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的符号类型:线程。 at evenCount.(Thread.java:16) at mainThread.main(Thread.java:56) /Users/user/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) at evenCount.(Thread.java:16) at mainThread.main(Thread.java:56) /Users/user/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (总时间:0秒)

Sometimes it shows build successfull but doesnt show the result(when i use Runnable)有时它显示构建成功但不显示结果(当我使用 Runnable 时)

It doesn't show any output, because your for loop condition is wrong.它没有显示任何 output,因为您的 for 循环条件错误。

for(int i = 2; i >=20; i++) and for(int i = 21; i>=31; i++) for(int i = 2; i >=20; i++)for(int i = 21; i>=31; i++)

both for-loop never get through.两个 for 循环都不会通过。

It should be i<=20 and i<=31它应该是i<=20i<=31

At least with above change, you will get output.至少通过上述更改,您将获得 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM