Exception call back : Utilities « Threads « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Threads » UtilitiesScreenshots 
Exception call back
Exception call back


import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class ExceptionCallbackMain extends Object implements ExceptionListener {

  private int exceptionCount;

  public ExceptionCallbackMain() {
    exceptionCount = 0;
  }

  public void exceptionOccurred(Exception x, Object source) {
    exceptionCount++;
    System.err.println("EXCEPTION #" + exceptionCount + ", source="
        + source);
    x.printStackTrace();
  }

  public static void main(String[] args) {
    ExceptionListener xListener = new ExceptionCallbackMain();
    ExceptionCallback ec = new ExceptionCallback(xListener);
  }
}

class ExceptionCallback extends Object {
  private Set exceptionListeners;

  private Thread internalThread;

  private volatile boolean noStopRequested;

  public ExceptionCallback(ExceptionListener[] initialGroup) {
    init(initialGroup);
  }

  public ExceptionCallback(ExceptionListener initialListener) {
    ExceptionListener[] group = new ExceptionListener[1];
    group[0= initialListener;
    init(group);
  }

  public ExceptionCallback() {
    init(null);
  }

  private void init(ExceptionListener[] initialGroup) {
    System.out.println("initializing...");

    exceptionListeners = Collections.synchronizedSet(new HashSet());

    if (initialGroup != null) {
      for (int i = 0; i < initialGroup.length; i++) {
        addExceptionListener(initialGroup[i]);
      }
    }

    noStopRequested = true;

    Runnable r = new Runnable() {
      public void run() {
        try {
          runWork();
        catch (Exception x) {
          sendException(x);
        }
      }
    };

    internalThread = new Thread(r);
    internalThread.start();
  }

  private void runWork() {
    try {
      makeConnection()
    catch (IOException x) {
      sendException(x);
    }

    String str = null;
    int len = determineLength(str)
  }

  private void makeConnection() throws IOException {
    String portStr = "Java2s20";
    int port = 0;

    try {
      port = Integer.parseInt(portStr);
    catch (NumberFormatException x) {
      sendException(x);
      port = 80;
    }

    connectToPort(port)
  }

  private void connectToPort(int portNumthrows IOException {
    throw new IOException("connection refused");
  }

  private int determineLength(String s) {
    return s.length();
  }

  public void stopRequest() {
    noStopRequested = false;
    internalThread.interrupt();
  }

  public boolean isAlive() {
    return internalThread.isAlive();
  }

  private void sendException(Exception x) {
    if (exceptionListeners.size() == 0) {
      x.printStackTrace();
      return;
    }

    synchronized (exceptionListeners) {
      Iterator iter = exceptionListeners.iterator();
      while (iter.hasNext()) {
        ExceptionListener l = (ExceptionListeneriter.next();

        l.exceptionOccurred(x, this);
      }
    }
  }

  public void addExceptionListener(ExceptionListener l) {
    if (l != null) {
      exceptionListeners.add(l);
    }
  }

  public void removeExceptionListener(ExceptionListener l) {
    exceptionListeners.remove(l);
  }

  public String toString() {
    return getClass().getName() "[isAlive()=" + isAlive() "]";
  }
}

interface ExceptionListener {
  public void exceptionOccurred(Exception x, Object source);
}

           
       
Related examples in the same category
1. View current Threads in a table View current Threads in a table
2. Listing all threads and threadgroups in the VM.Listing all threads and threadgroups in the VM.
3. Early returnEarly return
4. Transition DetectorTransition Detector
5. Busy Flag
ww__w.__j__a__v_a_2__s_.___co_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.