Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1336 > unrolled thread

calling SwingWorker cance

Started by"yancheng.cheok" <yancheng.cheok@THRWHITE.remove-dii-this>
First post2011-04-27 15:32 +0000
Last post2011-04-27 15:32 +0000
Articles 4 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  calling SwingWorker cance "yancheng.cheok" <yancheng.cheok@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
    Re: calling SwingWorker c "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
      Re: calling SwingWorker c "yancheng.cheok" <yancheng.cheok@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
        Re: calling SwingWorker c "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000

#1336 — calling SwingWorker cance

From"yancheng.cheok" <yancheng.cheok@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
Subjectcalling SwingWorker cance
Message-ID<1175356201.064989.263820@p15g2000hsd.googlegroups.com>
  To: comp.lang.java.gui
hello,

i realize that when after i call SwingWorker cancel and try to wait
until the task is really canceled (through SwingWorker.get), i will
get exception as stated in http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html

Note: If get is invoked on a SwingWorker object after its background
task has been cancelled, java.util.concurrent.CancellationException is
thrown.

Hence, i was wondering after the CancellationException caught, when
the time the next statement is executed, does the task is really
cancelled?

thanks

    private void formWindowClosing(java.awt.event.WindowEvent evt)
{
// TODO add your handling code here:
        try
        {
            marketTask.cancel(false);
            marketTask.get();
        }
        catch(InterruptedException exp) {
        }
        catch(java.util.concurrent.ExecutionException exp)
{
        }
        catch(java.util.concurrent.CancellationException exp)
{
        }

// So, does market Task really cancelled???
    }

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1337 — Re: calling SwingWorker c

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: calling SwingWorker c
Message-ID<GpwPh.160048$ia7.153256@newsfe14.lga>
In reply to#1336
  To: comp.lang.java.gui
yancheng.cheok@gmail.com wrote:
> hello,
> 
> i realize that when after i call SwingWorker cancel and try to wait
> until the task is really canceled (through SwingWorker.get), i will
> get exception as stated in http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html
> 
> Note: If get is invoked on a SwingWorker object after its background
> task has been cancelled, java.util.concurrent.CancellationException is
> thrown.
> 
> Hence, i was wondering after the CancellationException caught, when
> the time the next statement is executed, does the task is really
> cancelled?
> 
> thanks
> 
>     private void formWindowClosing(java.awt.event.WindowEvent evt)
> {
> // TODO add your handling code here:
>         try
>         {
>             marketTask.cancel(false);
>             marketTask.get();
>         }
>         catch(InterruptedException exp) {
>         }
>         catch(java.util.concurrent.ExecutionException exp)
> {
>         }
>         catch(java.util.concurrent.CancellationException exp)
> {
>         }
> 
> // So, does market Task really cancelled???
>     }
> 

You will know that marketTask is canceled by checking the return value 
of cancel().  Calling get() will not block.  It will just throw a 
CancellationException.

 From the docs:

cancel

public final boolean cancel(boolean mayInterruptIfRunning)

     Attempts to cancel execution of this task. This attempt will fail 
if the task has already completed, has already been cancelled, or could 
not be cancelled for some other reason. If successful, and this task has 
not started when cancel is called, this task should never run. If the 
task has already started, then the mayInterruptIfRunning parameter 
determines whether the thread executing this task should be interrupted 
in an attempt to stop the task.

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1360 — Re: calling SwingWorker c

From"yancheng.cheok" <yancheng.cheok@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: calling SwingWorker c
Message-ID<1175434540.675174.116000@n59g2000hsh.googlegroups.com>
In reply to#1337
  To: comp.lang.java.gui
is there anyway, i can wait for the swing worker to really complete,
after i call the cancel method? this is my swing worker will only
terminate, only if i call cancel()...

            while (! isCancelled()) {
		// swing worker is doing something....
            }

>
> You will know that marketTask is canceled by checking the return value
> of cancel().  Calling get() will not block.  It will just throw a
> CancellationException.

Is there any way I can "wait", until the task is really completed
after I make a call to cancel? My task is running in a infinity loop :


>  From the docs:
>
> cancel
>
> public final boolean cancel(boolean mayInterruptIfRunning)
>
>      Attempts to cancel execution of this task. This attempt will fail
> if the task has already completed, has already been cancelled, or could
> not be cancelled for some other reason. If successful, and this task has
> not started when cancel is called, this task should never run. If the
> task has already started, then the mayInterruptIfRunning parameter
> determines whether the thread executing this task should be interrupted
> in an attempt to stop the task.
>
> --
>
> Knute Johnson
> email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1364 — Re: calling SwingWorker c

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: calling SwingWorker c
Message-ID<_2SPh.286892$Ju2.128092@newsfe16.lga>
In reply to#1360
  To: comp.lang.java.gui
yancheng.cheok@gmail.com wrote:
> is there anyway, i can wait for the swing worker to really complete,
> after i call the cancel method? this is my swing worker will only
> terminate, only if i call cancel()...
> 
>             while (! isCancelled()) {
> 		// swing worker is doing something....
>             }

No.

>> You will know that marketTask is canceled by checking the return value
>> of cancel().  Calling get() will not block.  It will just throw a
>> CancellationException.
> 
> Is there any way I can "wait", until the task is really completed
> after I make a call to cancel? My task is running in a infinity loop :

If your task is an infinite loop then you don't need SwingWorker.  Just 
use a Runnable and start it running with a thread.  Then when you are 
done, signal it to stop either by interrupting it, setting a flag or 
causing some sort of exception to be thrown.

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

public class test5 implements Runnable {
     volatile boolean runFlag = true;
     Thread thread;

     public void run() {
         // once started this thread will run as long as runFlag is true
         while (runFlag) {
             // do your processing here, this example just sleeps
             System.out.println("processing...");
             try {
                 Thread.sleep(3000);
             } catch (InterruptedException ie) { }
         }
         System.out.println("process stopped");
     }

     public static void main(String[] args) {
         // create the gui
         Runnable r = new Runnable() {
             public void run() {
                 final test5 t5 = new test5();
                 final JFrame f = new JFrame();
                 // leave the window open when the X is clicked
                 f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                 f.addWindowListener(new WindowAdapter() {
                     public void windowClosing(WindowEvent we) {
                         // start another thread here so as not to
                         //  block the EDT
                         Runnable r = new Runnable() {
                             public void run() {
                                 // tell the loop to end
                                 t5.runFlag = false;
                                 // join this thread and the task thread
                                 // this will wait here until the other
                                 //  thread is finished or this thread is
                                 //  interrupted
                                 try {
                                     t5.thread.join();
                                 } catch (InterruptedException ie) { }
                                 // close the program
                                 System.exit(0);
                             }
                         };
                         // start the runnable above to end the program
                         new Thread(r).start();
                         // show a dialog to let users know what is
                         //  happening
                         JDialog d = new JDialog(f,false);
                         d.setLocationRelativeTo(f);
                         JLabel l = new JLabel("Shutting Down");
                         d.add(l);
                         d.pack();
                         d.setVisible(true);
                     }
                 });
                 f.setSize(200,150);
                 f.setVisible(true);

                 // start the task running
                 t5.thread = new Thread(t5);
                 t5.thread.start();
             }
         };
         EventQueue.invokeLater(r);
     }
}

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web