简体   繁体   English

在简单的Java程序和Java Web应用程序中的线程通信

[英]Thread communication In simple java program and in Java web application

How thread communication work is different in web application and Simple java Program. Web应用程序和简单Java程序中线程通信的工作方式不同。 Please guide me on below query. 请引导我进行以下查询。 Try to narrate question please ignore grammatical mistake. 尝试叙述问题,请忽略语法错误。

Suppose i have simple java class 假设我有简单的Java类

package com.scjp.chapter.Exception;
public class ThreadException extends Thread{
        public void run(){
        show();
        display();
    }

    private void display() {
        System.out.println(ThreadException.currentThread().getName() + " Display");
    }

    private void show() {
        int i = 10/0;
        System.out.println(i);
    }

    public static void main(String[] args) {

        ThreadException thread1 = new ThreadException();
        ThreadException thread2 = new ThreadException();
        ThreadException thread3= new ThreadException();

        thread1.setName("thread1");
        thread2.setName("thread2");
        thread3.setName("thread3");

        thread1.start();
        thread2.start();
        thread3.start();

    }
}

Above program will not print display content.All three is throwing show method Arithmetic exception. 上面的程序不会打印显示内容。所有三个都是抛出show方法的Arithmetic异常。 But if we call display() before to show(). 但是,如果我们在show()之前调用display()。 Then program is generating output of display() as well as throwing Arithmetic exception also.And output order is changing. 然后程序将生成display()的输出并引发算术异常,并且输出顺序正在更改。 So my question is why display() method output is not coming when will calling show() before display() ?. 所以我的问题是,为什么在display()之前调用show()时,display()方法的输出不会到来?

According to Exception concept below line should not execute after the exception throw so if thread1 is throwing exception then other thread two thread should not execute further code. 根据异常概念,以下行不应在异常引发后执行,因此,如果线程1引发异常,则其他线程两个线程不应执行进一步的代码。

why display() method output is not coming when will calling show() before display() ? 为什么在display()之前调用show()时,display()方法的输出不会到来?

Because show() throws an exception, which thus causes the method execution to stop. 由于show()引发异常,因此导致方法执行停止。

if thread1 is throwing exception then other thread two thread should not execute further code 如果thread1引发异常,则其他两个线程不应执行进一步的代码

Yes, it should, because a each thread has its own... thread of execution. 是的,应该,因为每个线程都有自己的执行线程。 Its instructions are executed in parallel to the other threads of execution, and the exception thrown in a thread doesn't have any impact on a separate thread. 它的指令与其他执行线程并行执行,并且在线程中引发的异常对单独的线程没有任何影响。 That's basically the whole point of threads. 基本上,这就是线程的全部要点。

How thread communication work is different in web application and Simple java Program Web应用程序和简单Java程序中线程通信的工作方式不同

It's not. 不是。

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

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