Erro sinistro com threadPool

Ola, tenho um programa que executa 2 threads

estou tentando usar ThreadPool para controla-las e finaliza-las de uma vez conforme o codigo abaixo



import java.util.concurrent.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class exemplo extends JFrame{
	
private static final long serialVersionUID = 1L;
private ExecutorService tpes =  null;	
private JButton inic = null, para = null;	
private JPanel p = null;
private boolean exec = true;
private Runnable r1 = null,r2 = null;


//construtor
exemplo(){
	
setSize(200,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

tpes = Executors.newCachedThreadPool();


inic = new JButton("Iniciar");
inic.addActionListener(
 new ActionListener(){
  public void actionPerformed(ActionEvent e){
	  
	  exec =  true;
	  
	  tpes.execute(r1);
	  tpes.execute(r2);
	  	
  }});

para = new JButton("Parar");
para.addActionListener(
 new ActionListener(){
  public void actionPerformed(ActionEvent e){

	 tpes.shutdownNow();
	  
	 
  }});

p = new JPanel();
p.add(inic);
p.add(para);

getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,"Center");

setVisible(true);

r1 = new Runnable(){
	 
    public void run(){
   	 
    while(exec){     	 
    
    try{ Thread.sleep(2000);
         System.out.println("thread 1");  
         
        }catch(Exception rt){
       	   rt.printStackTrace();
       	   exec = false; }

    }//while     
         
    }//run     
         
};//r1
	
r2 = new Runnable(){
	 
    public void run(){
   	 
    while(exec){     	 
    
    try{ Thread.sleep(4000);
         System.out.println("thread 2");  
         
        }catch(Exception rt){
       	 rt.printStackTrace();  
       	   exec = false; }

    }//while     
         
    }//run     
         
};//r1


 }//end of constructor	
	
public static void main(String args[]){
	
new exemplo();	
	
}//end of main	
	
}//end of class

quando eu tento finalizar uma thread, e gerado um erro, mas esse erro esta sendo provocado por mim e é para terminar as threads imediatamente


java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at exemplo$3.run(exemplo.java:114)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at exemplo$4.run(exemplo.java:133)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

agora se eu clicar no botao iniciar para fazer as threads rodarem outra vez , da um erro que eu nao sei a causa


Exception in thread "AWT-EventQueue-0" java.util.concurrent.RejectedExecutionException
	at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.reject(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)
	at exemplo$1.actionPerformed(exemplo.java:41)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

ou seja , so consigo executar o codigo uma vez, alguem pode me ajudar ?

É assim mesmo. Pesquise sobre FutureTask:

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/FutureTask.html

package guj;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TesteFutureTask extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JPanel pnlButtons = null;
    private JButton btnStartTask1 = null;
    private JButton btnStartTask2 = null;
    private JButton btnStopTask1 = null;
    private JButton btnStopTask2 = null;
    private JButton btnStopAllTasks = null;
    private JPanel pnlLabels = null;
    private JLabel lblTask1 = null;
    private JLabel lblTask2 = null;
    private JLabel lblMessages = null;

    private ExecutorService executor;
    private FutureTask<Void> task1;
    private FutureTask<Void> task2;
    private Callable<Void> callable1;
    private Callable<Void> callable2;

    private JPanel getPnlButtons() {
        if (pnlButtons == null) {
            pnlButtons = new JPanel();
            pnlButtons.setLayout(new FlowLayout());
            pnlButtons.add(getBtnStartTask1(), null);
            pnlButtons.add(getBtnStartTask2(), null);
            pnlButtons.add(getBtnStopTask1(), null);
            pnlButtons.add(getBtnStopTask2(), null);
            pnlButtons.add(getBtnStopAllTasks(), null);
        }
        return pnlButtons;
    }

    private JButton getBtnStartTask1() {
        if (btnStartTask1 == null) {
            btnStartTask1 = new JButton();
            btnStartTask1.setText("Start Task 1");
            btnStartTask1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    btnStartTask1.setEnabled(false);
                    btnStopTask1.setEnabled(true);
                    btnStopAllTasks.setEnabled(true);
                    callable1 = new Callable<Void>() {
                        @Override
                        public Void call() throws Exception {
                            try {
                                while (!task1.isCancelled()) {
                                    lblTask1.setText(String.format("Task 1: %d", i.getAndIncrement()));
                                    Thread.sleep(200);
                                }
                            } finally {
                                btnStartTask1.setEnabled(true);
                                btnStopTask1.setEnabled(false);
                                if (!btnStopTask2.isEnabled())
                                    btnStopAllTasks.setEnabled(false);
                            }
                            return null;
                        }

                        private AtomicInteger i = new AtomicInteger(0);
                    };
                    task1 = new FutureTask<Void>(callable1);
                    executor.submit(task1);
                }
            });
        }
        return btnStartTask1;
    }

    private JButton getBtnStartTask2() {
        if (btnStartTask2 == null) {
            btnStartTask2 = new JButton();
            btnStartTask2.setText("Start Task 2");
            btnStartTask2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    btnStartTask2.setEnabled(false);
                    btnStopTask2.setEnabled(true);
                    btnStopAllTasks.setEnabled(true);
                    callable2 = new Callable<Void>() {
                        @Override
                        public Void call() throws Exception {
                            try {
                                while (!task2.isCancelled()) {
                                    lblTask2.setText(String.format("Task 2: %d", i.getAndIncrement()));
                                    Thread.sleep(200);
                                }
                            } finally {
                                btnStartTask2.setEnabled(true);
                                btnStopTask2.setEnabled(false);
                                if (!btnStopTask1.isEnabled())
                                    btnStopAllTasks.setEnabled(false);
                            }
                            return null;
                        }

                        private AtomicInteger i = new AtomicInteger(0);
                    };
                    task2 = new FutureTask<Void>(callable2);
                    executor.submit(task2);
                }
            });
        }
        return btnStartTask2;
    }

    private JButton getBtnStopTask1() {
        if (btnStopTask1 == null) {
            btnStopTask1 = new JButton();
            btnStopTask1.setText("Stop Task 1");
            btnStopTask1.setEnabled(false);
            btnStopTask1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (!(task1.isDone() || task1.isCancelled()))
                        task1.cancel(true);
                }
            });
        }
        return btnStopTask1;
    }

    private JButton getBtnStopTask2() {
        if (btnStopTask2 == null) {
            btnStopTask2 = new JButton();
            btnStopTask2.setText("Stop Task 2");
            btnStopTask2.setEnabled(false);
            btnStopTask2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (!(task2.isDone() || task2.isCancelled()))
                        task2.cancel(true);
                }
            });
        }
        return btnStopTask2;
    }

    private JButton getBtnStopAllTasks() {
        if (btnStopAllTasks == null) {
            btnStopAllTasks = new JButton();
            btnStopAllTasks.setText("Stop All Tasks");
            btnStopAllTasks.setEnabled(false);
            btnStopAllTasks.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (!(task1.isDone() || task1.isCancelled()))
                        task1.cancel(true);
                    if (!(task2.isDone() || task2.isCancelled()))
                        task2.cancel(true);
                }
            });
        }
        return btnStopAllTasks;
    }

    private JPanel getPnlLabels() {
        if (pnlLabels == null) {
            lblMessages = new JLabel();
            lblMessages.setText(".");
            lblTask2 = new JLabel();
            lblTask2.setText(".");
            lblTask1 = new JLabel();
            lblTask1.setText(".");
            pnlLabels = new JPanel();
            pnlLabels.setLayout(new BoxLayout(getPnlLabels(), BoxLayout.Y_AXIS));
            pnlLabels.add(lblTask1, null);
            pnlLabels.add(lblTask2, null);
            pnlLabels.add(lblMessages, null);
        }
        return pnlLabels;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TesteFutureTask thisClass = new TesteFutureTask();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }

    public TesteFutureTask() {
        super();
        initialize();

        executor = Executors.newCachedThreadPool(); // @jve:decl-index=0:
    }

    private void initialize() {
        this.setSize(707, 145);
        this.setContentPane(getJContentPane());
        this.setTitle("Teste FutureTask");
    }

    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getPnlButtons(), BorderLayout.SOUTH);
            jContentPane.add(getPnlLabels(), BorderLayout.CENTER);
        }
        return jContentPane;
    }

} // @jve:decl-index=0:visual-constraint="10,10"

Outro exemplo com Future e Callable: