English 中文(简体)
Java:允许儿童在受到干扰的欺骗时 kill杀自己?
原标题:Java: Allowing the child thread to kill itself on InterruptedException?

我正在通过Executor Service使用ThreadPool。 该公司打电话,打断了池内的所有 running。 当出现这种情况时,我想这些线索放弃其资源(袖珍和干.连接),而只是死去,但又不继续维持任何逻辑,例如:在非行插入任何东西。 实现这一目标的最简单途径是什么? 下面是一些样本代码:

<>strong>edit: forgot to refer that all my connections are release through finally. 我只需要确保,以可靠的方式,用不同的行文来达到这一目的。

public void threadTest() {
    Thread t = new Thread(new Runnable() {

            public void run() {
                try {
                    Thread.sleep(999999);
                } catch (InterruptedException e) {
                    //invoke thread suicide logic here
                }
            }
        });
    t.start();

    t.interrupt();
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
    }
}
最佳回答

我认为,这样做的坦率方式从来都不涉及<>捕获><>/em>干扰的渗透。

不管这个问题如何,你总是在<条码>中放下你的资源。 在此情况下,你们都需要做的是,让干扰的视像泡沫达到顶级(即,在<代码>第上公布)。 clause!

有些人可能会ba忙在所有这些方法中添加一个经过核实的例外。 然而,这在概念上是正确的——一种宣布放弃<条码>的不间断的Exception的方法表明,这是一种阻塞方法。 通常,只有低水平的方法才能实施,因为高层次的方法很少最终涉及封锁(即可以提出可行的备选执行/取代不需要的另一种依赖)。

确实值得怀疑的是,你想要使你的法典像这样“合理停止”。 您没有“ ly > ,其背景是,由read管理人无条件地做这样的事情,而代之的守则需要与你的努力合作。 放弃中断的例外情况泡沫是一种办法;另一种办法可以是追捕这一相对较低的例外情况,并设定一个你在较高层次的法典中投票的蓝色<编码>。

无论如何,这里的问题完全在于如何获得更高层次的法典,以知道它应当在这种情况下终止。 如果按您的原样将资源关闭在<代码>中,则你知道,不管什么东西,总是会适当地释放这些资源。

问题回答

请注意,如果出现不可中断的阻塞(例如,等待监测才能释放或等待国际独立组织行动)的情况,将不帮助打上警钟。

在这些情况下,你必须使用明确的 j。 洛克比(可打断)而不是同步区块和 j瓦。 io

当胎面完满时,就会自动死亡。 因此,实际上没有:

public void threadTest() {
    Thread t = new Thread(new Runnable() {

            public void run() {
                try {
                    Thread.sleep(999999);
                } catch (InterruptedException e) {
                    //invoke thread suicide logic here
                    // Do nothing so the thread will die
                }
            }
        });
    t.start();

    t.interrupt();
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
    }
}

读物在读书结束时将免于记忆,因为read形变数将不再提及。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签