1

I got a Socket Server running as a Java SwingWorker.

The SwingWorker receives incoming connections, and upon connection, hands over the socket to an Executor that takes care of the connection. The Executor calls a SocketHandler that will handle the socket via the run() method.

Now, I have one button on the GUI that says stop. I want to stop the SwingWorker from execution when i press the Stop button. To do so, I had a cancel(true) method called upon pressing the Stop button.

Now, the SocketHandler continues receiving incoming data despite my program calling the cancel(true) method. What's going on here?

I had read thru couple articles that it's a bug within the JDK that calling cancel(true) method does not stop the SwingWorker. Is this true?

Poliquin
  • 2,937
  • 4
  • 28
  • 32

3 Answers3

4

simply add a boolean checker in the loop

if (!stillWorking)return;

then you can set the boolean to false to stop the method.

Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
  • Hi stas, I have not tried it because i was busy with work. (I am not a full time programmer). I will test it over the next week and update you. Thanks! – Poliquin Jun 10 '11 at 12:26
3

API for public final boolean cancel(boolean mayInterruptIfRunning):

"mayInterruptIfRunning - true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete".

You have to read on Thread.interrupt. It actually does not interrupt anything; you have to specify in your code how to deal with interrupts.

Also, it's not clear from your question if you just want to stop the main SwingWorker from taking connections or if you want to kill all existing connections too.

EDIT: If you do have a while loop in the thread accepting the connections:

while (!Thread.interrupted()) {

should be sufficient, I guess. Now when the SwingWorker is cancelled, the loop should stop because the SwingWorker thread has it inner interrupt flag set.

toto2
  • 5,306
  • 21
  • 24
2

@Yakult121 please this isn't answer to your both questions Java SwingWorker Socket Server does not get cancelled when i cancel the the SwingWorker and Java SwingWorker - using publish/process to update a TextArea in EDT? , I think that your question not fully described, I miss there lots of importan details,

you have to post code for better ..., that demostrated your implementations for Executor, SwingWorker and cancelation for that

your code could be based on this two threads, too

1/ Cancel SwingWorker

2/ Executor And SwingWorker

3/ and how/what you determine concrete SwingWorker's thread for cancelations

4/ really put your two thread together

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Hi mKorbel, initially they were part of the same question. But after posting the other thread, i was recommended to use an ExecutorService to run the socket server, instead of using a SwingWorker. I'm very confused now. Can you help me understand why? – Poliquin Jun 10 '11 at 12:25
  • Yakult121 there is about your code, to shows what you are tried, really your two topic doesn's make me any idea, and nobody knows how your code works too, your code example for this topic should be based on links that I posted, – mKorbel Jun 10 '11 at 12:33
  • Hi mKorbel, I have continued to play with Java swing and have found the answer. I dropped SwingWorker and is working with Executor directly. I have not tried the thread cancellation yet. – Poliquin Jun 13 '11 at 20:16