---
title: 'Restart a died Thread'
url: 'https://tutego.de/blog/javainsel/2007/01/restart-a-died-thread'
markdown: 'https://tutego.de/blog/javainsel/2007/01/restart-a-died-thread.md'
date: '2007-01-21'
description: 'If a Thread dies the system can inform you via a Thread.UncaughtExceptionHandler. You can use it to restart a Thread: import java.awt.Frame;import java.io.PrintWriter;import java.io.StringWriter;import java.lang.Thread.UncaughtExceptionHandler;import javax.swing.JDialog;import javax.swing.JTextArea…'
taxonomy:
  category:
    - Allgemein
---

# Restart a died Thread

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## Restart a died Thread

21.01.2007 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

If a `Thread` dies the system can inform you via a `Thread`.`UncaughtExceptionHandler`. You can use it to restart a `Thread`:

```java
import java.awt.Frame;import java.io.PrintWriter;import java.io.StringWriter;import java.lang.Thread.UncaughtExceptionHandler;import javax.swing.JDialog;import javax.swing.JTextArea;public class Main{  public static void main( String[] args )  {    ThreadHandler.startThread();  }}class ThreadHandler{  static void startThread()  {    Thread t = new Thread( new MyCommand() );    UncaughtExceptionHandler ueh = new MyUncaughtExceptionHandler();    t.setUncaughtExceptionHandler( ueh );    t.start();  }  static class MyCommand implements Runnable  {    public void run()    {      int i = 0;      System.out.println( 12 / i ); // Provoke a RuntimeException    }  }}class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{  public void uncaughtException( Thread thread, Throwable throwable )  {    StringWriter stringWriter = new StringWriter();    PrintWriter w = new PrintWriter( stringWriter );    throwable.printStackTrace( w );    JDialog d = new JDialog( new Frame(), true );    d.add( new JTextArea( stringWriter.toString() ) );    d.setTitle( thread.getName() + " --- " + throwable.getMessage() );    d.pack();    d.setVisible( true );    ThreadHandler.startThread(); // Restart Thread  }}
```

Kategorien: [Allgemein](https://tutego.de/blog/javainsel/category/allgemein/)

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [Buchkritik "Java 6 Plattform Revealed"](https://tutego.de/blog/javainsel/2007/01/buchkritik-java-6-plattform-revealed.md)
- Next: [AJAX in 50 simple lines: Requesting HTML and Producing JSP](https://tutego.de/blog/javainsel/2007/01/ajax-in-50-simple-lines-requesting-html-and-producing-jsp.md)
