Exception in java thread

Leo

Consider the following code:

public class ThreadT implements Runnable {

 public void run() {
 System.out.println("run.");
 throw new RuntimeException("Problem");
 }
 public static void main(String[] args) {
 Thread t = new Thread(new ThreadT());
 t.start();
 System.out.println("End of method.");
 }
}

The output I get is:

End of method.
run.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem

Why the output is not like this:

run.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
End of method.
MoOoG

I believe it's because when you run a new thread beside Main, both threads run at the same time and first it prints out the "End of method", right after it runs the second outprint (which is probably few miliseconds behind.

try this:

public class thread implements Runnable {

public void run() {
System.out.println("run.");
throw new RuntimeException("Problem");
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new thread());
t.start();
Thread.sleep(3000);
System.out.println("End of method.");
}
}

What I did here is to put Main thread into sleep for 3 seconds and give time to Thread t to run first.

Or you can also write join() to wait the Thread to finish first. Like this:

public class thread implements Runnable {

public void run() {
System.out.println("run.");
throw new RuntimeException("Problem");
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new thread());
t.start();
t.join();
System.out.println("End of method.");
}
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

java error: Exception in thread "main" java.lang.NoClassDefFoundError

分類Dev

Exception in thread "main" java.lang.NoClassDefFoundError: jcuda/driver/JCudaDriver

分類Dev

Exception in thread "main" java.util.NoSuchElementException on ideone

分類Dev

Exception in Rest Template : Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/log/LogDelegateFactory

分類Dev

Exception in child thread

分類Dev

Exception in thread "main":NullPointerException

分類Dev

Canvas InvalidateVisual() thread exception

分類Dev

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space while using util Packages

分類Dev

Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanne

分類Dev

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper on maven

分類Dev

Exception in thread "main" java.lang.NumberFormatException: For input string: "t1"?

分類Dev

Why does Spark application fail with "Exception in thread "main" java.lang.NoClassDefFoundError: ...StringDeserializer"?

分類Dev

Exception in thread "main" java.lang.NoClassDefFoundError: com/twitter/chill/KryoBase

分類Dev

Flutter: Exception in thread "main" java.util.zip.ZipException: error in opening zip file

分類Dev

Exception in thread "main" java.security.UnrecoverableKeyException: Given final block not properly padded

分類Dev

Exception in thread "main" java.lang.NoSuchMethodException: after I add ArrayList

分類Dev

Exception in thread "main" java.lang.UnsatisfiedLinkError: 'void org.lwjgl.opengl.WindowsDisplay.setWindowProc(java.lang.reflect.Method)'

分類Dev

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64

分類Dev

I have a error Exception in thread "main" java.lang.NullPointerException when I connect SQL Server and Check User in table

分類Dev

Exception handling : Thread v/s Task

分類Dev

How to kill process if thread encounters exception?

分類Dev

Java Stop Server Thread

分類Dev

run thread respectively in java

分類Dev

Java Thread argument

分類Dev

Java Thread.sleep()

分類Dev

Is throwing an Exception expensive in Java?

分類Dev

Exception Handling Java

分類Dev

Java NullPointer Exception in doInBackground

分類Dev

Exception questions Java

Related 関連記事

  1. 1

    java error: Exception in thread "main" java.lang.NoClassDefFoundError

  2. 2

    Exception in thread "main" java.lang.NoClassDefFoundError: jcuda/driver/JCudaDriver

  3. 3

    Exception in thread "main" java.util.NoSuchElementException on ideone

  4. 4

    Exception in Rest Template : Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/log/LogDelegateFactory

  5. 5

    Exception in child thread

  6. 6

    Exception in thread "main":NullPointerException

  7. 7

    Canvas InvalidateVisual() thread exception

  8. 8

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space while using util Packages

  9. 9

    Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanne

  10. 10

    Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper on maven

  11. 11

    Exception in thread "main" java.lang.NumberFormatException: For input string: "t1"?

  12. 12

    Why does Spark application fail with "Exception in thread "main" java.lang.NoClassDefFoundError: ...StringDeserializer"?

  13. 13

    Exception in thread "main" java.lang.NoClassDefFoundError: com/twitter/chill/KryoBase

  14. 14

    Flutter: Exception in thread "main" java.util.zip.ZipException: error in opening zip file

  15. 15

    Exception in thread "main" java.security.UnrecoverableKeyException: Given final block not properly padded

  16. 16

    Exception in thread "main" java.lang.NoSuchMethodException: after I add ArrayList

  17. 17

    Exception in thread "main" java.lang.UnsatisfiedLinkError: 'void org.lwjgl.opengl.WindowsDisplay.setWindowProc(java.lang.reflect.Method)'

  18. 18

    Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64

  19. 19

    I have a error Exception in thread "main" java.lang.NullPointerException when I connect SQL Server and Check User in table

  20. 20

    Exception handling : Thread v/s Task

  21. 21

    How to kill process if thread encounters exception?

  22. 22

    Java Stop Server Thread

  23. 23

    run thread respectively in java

  24. 24

    Java Thread argument

  25. 25

    Java Thread.sleep()

  26. 26

    Is throwing an Exception expensive in Java?

  27. 27

    Exception Handling Java

  28. 28

    Java NullPointer Exception in doInBackground

  29. 29

    Exception questions Java

ホットタグ

アーカイブ