简体   繁体   English

在 java 中编写线程同步代码时,我遇到了以下错误

[英]While writing a code for thread synchronization in java i cam across the following error

So I am a beginner in java.所以我是java的初学者。

Yesterday while writing a code for thread synchronization I came across an error java.util.NoSuchElementException .昨天在编写线程同步代码时,我遇到了一个错误java.util.NoSuchElementException Code and the error are specified below.代码和错误在下面指定。 Here the shared variable among threads is a .这里线程之间的共享变量是a From what I can guess from the error the variable b is having problems in taking the second value.从我可以从错误中猜到的变量b在取第二个值时遇到问题。


        class Sum
        {
            synchronized void addition(int a)
            {
                Scanner sc = new Scanner(System.in);
                System.out.println("enter vale of b: ");
                int b = sc.nextInt();
                sc.close();
                int c = a + b;
                System.out.println(c);
            }
        }
        class ThreadP1 extends Thread
        {
            Sum s1;
            ThreadP1(Sum s1)
            {
                this.s1 = s1;
            }
            public void run()
            {
                Scanner sc1 = new Scanner(System.in);
                System.out.println("enter element a: ");
                s1.addition(sc1.nextInt());
                sc1.close();
            }
        }
        class ThreadP2 extends Thread
        {
            Sum s1;
            ThreadP2(Sum s1)
            {
                this.s1 = s1;
            }
            public void run()
            {
                Scanner sc2 = new Scanner(System.in);
                System.out.println("enter element a: ");
                s1.addition(sc2.nextInt());
                sc2.close();
            }
        }
    
    
    
        public class Synchronisation1 
        {
            public static void main(String args[])
            {
                Sum obj3 = new Sum();
                ThreadP1 t12 = new ThreadP1(obj3);
                t12.start();
                ThreadP2 t13 = new ThreadP2(obj3);
                t13.start();
            }
        }

Error错误

>Thread-1 java.util.NoSuchElementException  
        >>at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        >>at java.base/java.util.Scanner.next(Scanner.java:1594)   
        >>at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        >>at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        >>at Sum.addition(Synchronisation1.java:8)
        >>at ThreadP2.run(Synchronisation1.java:40)

As commented, by closing the Scanner , you close the System.in .正如所评论的,通过关闭Scanner ,您将关闭System.in The latter scanner will get an exception when it realizes System.in is closed.后一种扫描器在意识到System.in已关闭时会出现异常。

One way to fix it will be not to close the scanner.解决它的一种方法是不关闭扫描仪。

By the way, ThreadP1 , ThreadP2 seem to do the same thing.顺便说一句, ThreadP1ThreadP2似乎做同样的事情。 Consider creating 2 instances of the same class instead.考虑改为创建相同 class 的 2 个实例。

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

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