本文介绍了是否必须关闭扫描仪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,无论是否使用扫描仪关闭方法,我都能产生相同的输出.

import java.util.Scanner;
class CheckPassFail
{
    //Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise.
    static int k;
    static String str;
    public static void main(String arg[])
    {
        System.out.println("Enter the marks : ");
        Scanner typein = new Scanner(System.in);
        str = typein.nextLine();
        typein.close();                             //Scanner Close
        k = Integer.parseInt(str);
        if(k>=50)
        {
            System.out.println("PASS");
        }
        else
        {
            System.out.println("FAIL");
        }
    }
}
解决方案

简短答案:,但是在许多情况下非常建议. /p>

扫描仪可以处理某种资源.可以是String,但在许多情况下,它可以是标准输入通道,文件,网络流或其他资源.

对于stdin,这不是什么大问题. stdin通常与您的应用程序一起生存(和死亡).最好不要关闭流,因为也许另一个对象有兴趣从标准输入通道读取数据.

但是,如果您从文件/网络/驱动程序/...中读取信息,则表示Java首先询问操作系统是否可以使用该资源.这意味着资源已分配给应用程序.其他希望写入文件(或在程序应写入文件的情况下进行读写)的其他应用程序(至少是临时的)被拒绝访问该资源.因此,尽快放弃资源很有趣.

但是,最后一个方面不是必须的. Java虚拟机终止时,/垃圾收集器将关闭程序不再可访问的资源,从而结束传递.此外,如果您的Java应用程序被您,另一个程序等杀死(终止),则操作系统肯定会释放资源.

但是,尽快放弃资源是有礼貌的(不要太夸张地说,如果稍后再释放几毫秒,这不是一个大问题).此外,不及早放弃资源会使系统陷入 deadlock livelock 的状态.在出现死锁的情况下,两个程序正在等待某个资源再次变为可用,同时又保留了另一个资源.由于没有任何程序会放弃自己的资源,因此程序将永远等待.如果发生这种情况,则系统将卡住.

最终活动的资源还会导致额外的内存/CPU使用率.在所有操作系统都管理完这些资源之后,因此可以合理地假设要管理它们,它们将使用少量的CPU.在许多情况下,活动文件会(部分)加载到内存中.网络资源也是如此,...总的来说占用空间不是很大,但是您可以想象,如果所有程序都将保留资源直到它们终止,这将产生很大的影响.

With the below code I'm able to yield the same output with/without the scanner close method.

import java.util.Scanner;
class CheckPassFail
{
    //Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise.
    static int k;
    static String str;
    public static void main(String arg[])
    {
        System.out.println("Enter the marks : ");
        Scanner typein = new Scanner(System.in);
        str = typein.nextLine();
        typein.close();                             //Scanner Close
        k = Integer.parseInt(str);
        if(k>=50)
        {
            System.out.println("PASS");
        }
        else
        {
            System.out.println("FAIL");
        }
    }
}
解决方案

Short answer: No, but it is however in many cases very advisable to do so.

A scanner works on some kind of resource. This can be a String, but in many cases it is either the standard input channel, a file, network stream or another resource.

For stdin, that's not much of a problem. The stdin normally lives (and dies) with your application. It is better not to close the stream, since perhaps another object is interested in reading from the standard input channel.

If you however read from files/network/driver/... this means Java has asked the operating system first if it could use that resource. This means the resource is allocated to the application. Other applications that wish to write to the file (or read and write in case your program should write to the file) are (at least temporary) denied access to that resource. It is thus interesting to give up resources as soon as possible.

The last aspect is not obliged however. The Java virtual machine will when it is terminated/the garbage collector passes by close the resources that are no longer accessible by the program. Furthermore the operating system will definitely release the resources if your Java application is killed (terminated) by you, another program,...

It is however polite to give up resources as soon as possible (don't take it too literally it's not a huge problem if you release them a few (milli)seconds later). Furthermore not giving up resources early enough can get the system into a deadlock or livelock. In the case of a deadlock, two programs are waiting for a resource to become available again while holding the other resource. Since none of the programs give up their own resource, the programs will wait forever. If this situation occurs the system is stuck.

Finally active resources also result in additional memory/cpu usage. After all the OS manages these resources, so it is reasonable to assume that to manage them, they will a use a small amount of CPU. In many cases active files are loaded (partly) into memory. The same goes for network resources,... In general the footprint isn't very huge, but you can imagine if all programs would keep resources until they terminate, this would have a large impact.

这篇关于是否必须关闭扫描仪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:04