• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems interrupting a thread

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I have a main class coded in Netbeans. It codes for a GUI. It has a button on it which when I press invokes the following code:


Where the ThreadClass is a class containing the code:

In the main class, I have another button called 'Reset'. What I would like is for this thread to be interrupted when this button is pressed. So for, the code is:


But it can't see myThread. What do I need to solve this issue, i.e. stop the above thread having pressed the 'Reset' button?

BTW, I have tried placing the statements:

outside of the methods, at the top of the class. Although the resetButtonActionPerformed method can see it, when I interrupt the thread and try to press the 'myButton' button again to start a new thread, i get the following error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException

Thanks.

[ July 17, 2007: Message edited by: Sam Bluesman ]

[ July 19, 2007: Message edited by: Sam Bluesman ]
[ July 19, 2007: Message edited by: Sam Bluesman ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B][Sam]: BTW, I have tried placing the statements:

outside of the methods, at the top of the class. [/B]

Good, you do indeed need to declare myThread somewhere where the reset button can see it, and at the top of the class is a good place for that.

[Sam]: Although the resetButtonActionPerformed method can see it, when I interrupt the thread and try to press the 'myButton' button again to start a new thread, i get the following error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException


That's because after you interrupt it, the thread is dead. And once a thread is dead, you can't start it again. You can fix this by replacing myThread with a new Thread, when the reset button is pushed.

Incidentally it's needlessly confusing to have a class called ThreadClass which is not a Thread. And a variable called thisThread which, again, is not a thread. Please don't mislead your readers this way.

Also, the code to handle the interruption can be made much simpler. There's no need to return if you're already at the end of the method, and there's no need to throw and catch an exception when a simple while loop does the same thing:
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked! Thanks very much, and I'm sorry for my naming style.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again.

What I thought had working isnt doing quite what it should be:

After what Jim said, the following code is placed at the top of my Program class that codes for a GUI:

As described before, it has a jButton myButton on it which when I press invokes the following code (note is has changed):


The class containing the run() method was changed to the following:

I have changed the code in the restButton method to the following:

The problem I have is that the thread can be ran and stopped twice creating the desired output/effect. However, it doesnt seem to run the third time...why is this?

Thanks

[ July 19, 2007: Message edited by: Sam Bluesman ]

[ July 19, 2007: Message edited by: Sam Bluesman ]
[ July 19, 2007: Message edited by: Sam Bluesman ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't know, Sam. What effect does that other code have? Can you show more of it? The code you've shown here con't compile, with that try block with no catch or finally.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

1) Sorry about the try-catch. Should have actually read:

The //some code here sections contains code that issues commands to a server which can only deal with one thread at a time.

2) I think my problem is that im not creating a new thread correctly when I am pressing the resetButton, given the other code above, where the resetButton code is:



Please tell me if you need any more information.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you do need to call myThread.start() at some point. I would think that immediately after creating it (in the restart action handler) would be a good time to do that. I'm not sure how you could have gotten the reset button to appear to work even once, without a start().
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I only want the thread to start when the myButton is pressed. I want the thread to be interrupted when the resetButton is pressed...

Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I suggest putting the code to start a new thread in myButton's action handler, and putting the code to interrupt a thread in resetButton's action handler. Hint: it's possible to declare the variable myThread in one place, and set it and start the thread in a different place.

You may need to think about what happens if someone pushes myButton without having first pressed resetButton. Is that an error? Should it just be ignored? Should it pretend that you pressed resetButton followed by myButton? Should it start a second thread without stopping the first? I have no idea; those are your decisions to make.

By the way, couldn't myButton have name that gives some tiny clue what it's supposed to do? The name resetButton works well enough, but myButton is kind of useless.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim.

The myButton is actually called 'plotDataButton'. The idea is that when this is pressed it invokes the ClassForThread class which contains the run() method which contains code that plots data in a Gtk+ window. This has to be a separate thread because the GUI needs to be accessable by the user while the Gtk+ window showing statistical data plot is shown as this will not work otherwise.

While the plotDataButton is enabled, the resetButton is disabled. Once the plotDataButton is pressed, it becomes diabled and the resestButton becomes enabled. The plotDataButton will be enabled again once the user presses the resetButton which again becomes disabled subsequently.

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!