Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

FutureTask.cancel(true) does not work

807569Aug 10 2006 — edited Aug 11 2006
Hello people
I really need help with this enigmatical :) cancel(true). It seems that I misunderstand something.
Basically speaking, when I call cancel(true) it returns true, but the task does not stop! The task itself does not do anything special - just simple math calculations. Here is the test case with GUI.
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

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

public class FutureTaskTest extends JFrame {

	private JPanel jContentPane = null;
	private JButton jButtonStart = null;
	private JButton jButtonStop = null;
	private FutureTask futureTask;
	private ExecutorService executorService = Executors.newCachedThreadPool();
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;

	public FutureTaskTest() {
		super();
		
		Runnable r = new Runnable() {

			public void run() {
				
				for(;;) {
					
					jLabel.setText("I'm alive! " +
							System.currentTimeMillis() + " " +
							Thread.currentThread() + " " +
							Thread.currentThread().getState());
					
					Math.sin(Math.random());
					
				}
			}
			
		};
		
		futureTask = new FutureTask(r, null);
		
		initialize();
	}
	

	/**
	 * This method initializes jButtonStart	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStart() {
		if (jButtonStart == null) {
			jButtonStart = new JButton();
			jButtonStart.setText("Start");
			jButtonStart.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					executorService.execute(futureTask);
					
					
				}
			});
		}
		return jButtonStart;
	}

	/**
	 * This method initializes jButtonStop	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStop() {
		if (jButtonStop == null) {
			jButtonStop = new JButton();
			jButtonStop.setText("Stop");
			jButtonStop.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					jLabel1.setText("Stopping. Can be stopped? " + futureTask.cancel(true));
				}
			});
		}
		return jButtonStop;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("JLabel");
			jLabel = new JLabel();
			jLabel.setText("JLabel");
			jPanel = new JPanel();
			jPanel.setLayout(new BoxLayout(getJPanel(), BoxLayout.Y_AXIS));
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
		}
		return jPanel;
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		FutureTaskTest ftt = new FutureTaskTest();
		
		ftt.setVisible(true);
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(544, 277);
		this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJButtonStart(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getJButtonStop(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getJPanel(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 8 2006
Added on Aug 10 2006
10 comments
516 views