Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Thread not stopping correctly

 
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone
i have started to learn and use thread yesterday and im trying to create a thread with a method this method will return me a thread
like that


so i can start this thread by doing this


and end it with

why i am doing this well in my program i have three button one to play vs the ia when i click it will call this function and start the thread and an other one with pvp this is exactly the same as the ia except it will ask the player to do more thing and finally the button restart this will check if both thread are alive and if yes it will stop that thread
like that



but when im using my code and i click the button so stop the thread its not stopping correctly but its start perfectly normal i dont understand why can i start it but not end it properly

in my console =>



no printf in my console so the thread didnt stop and i dont know why
 
Saloon Keeper
Posts: 6976
164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calling interrupt() does not necessarily cause a Thread to be terminated (check its javadocs for the precise conditions), it merely sets the interrupted status. It is up to the Thread to check that status (by calling isInterrupted() at opportune times) and react accordingly (like by terminating itself).
 
Saloon Keeper
Posts: 4505
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Cyan,

biggest problem is this:

and

You see, everytime you call 'generate_pve', it will return a new Thread, so the first line above generates and starts Thread A, while the second line generates Thread B, and tries to interrupt it (while it hasn't even started!).
So, first thing to do is:

However, that will still not work. I come back to you later what is missing and whether things can be done in a slightly simpler way.



Edit: I see that Tim already pointed to the problem. Is there a way to stop the play_pve?
 
Saloon Keeper
Posts: 12997
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stopping threads externally usually won't work the way you want it to, and it's a bad idea to operate on threads directly to begin with.

The correct way to do this is to have your  play_pve() operation return by itself. If you want to make it return earlier, let it periodically check some shared state.

The interrupt() method is used to interrupt blocking operations. If your parallel task doesn't consist of blocking operations, calling interrupt() will be pointless.
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim and Piet thank for your reply and thank you for your very clear explanation but i dont understand what do you mean by stop play_pve play_pve run in a while loop until the game is done so if by stop you mean end the loop then no i dindnt think of a possibility where i need to stop the game like that that why i searched for a method to end the thread because if the thread is dead the method itself is dead

Tim thanks for your reply im reading the doc but i dont find what i search destroy and stop are Deprecated and join dont really do what i want either

and i want when i click to restart it end my thread and i click on pve after reset it will start a new thread
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan do you mean that i have something like a if(exit == false) it will not stop until the exit is true and when its true the method play_pve() will make an interupt() ?
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to make it clear what the logic of the button

press pve button launche the thread click on restart will end the thread and if the user press another time pve it will create a new thread to restart the game so Piet im using your suggestion but

and im not sur in how to make it like that here is my action listener for my button
 
Tim Moores
Saloon Keeper
Posts: 6976
164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somewhere in your code (the play_pve method), you need to call isInterrupted(), and if it returns true, you need to exit the method (which will cause the thread to die).

That's how you stop a thread - call interrupt externally, and then react to that by exiting the theead if isInterrupted ever returns true.
 
Piet Souris
Saloon Keeper
Posts: 4505
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Cyan,

I think you are doing things the complicated way! But lets go on for now.
The problem with this:

is that the generated Thread 'thread_pve' will block until play_pve returns. As such, it has no means of stopping play_pve! And even so, it does not stop play_pve in any way, so even if you could stop the thread, play_pve will still continue! That is why I asked if play_pve has any means of being stopped, for instance a method 'stop_pve()' that stops that pve. I have no idea what that pve does, so I can't suggest what you should do.

Must play_pve be run on another Thread? Otherwise you can call 'start_pve' directly in your actionListener. In that case you could have a second button with an actionlistener that stops pve, or you could change your actionlistener to something like:

I have made an example of a Thread that generates a Thread that calls a long during method, and then it waits until it is interrupted or until that long during operation is finished. The geretated Thread does not block, since that operation is a Runnable and is started on some other Thread. The meaning is trying to implement your starting idea. But you see how contrived it is: not something you would do just for the fun of it!
 
Piet Souris
Saloon Keeper
Posts: 4505
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim

you are too fast for me!    
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok with your reply i have changed my method like that but im not sure if i have make the good change

first my var thread are in global static global because i cant make it in my listener and i dont really have other method to make it and static because if i want to interupt it in my method play_pve i need to make it in static



now if i click in my play pve the thread will start


and the method pve will launch


and if i click at restart


Tim and Piet i have tried your solution but its not working and i dont really get why if my thread is interupted i will check it at every action in my while and break and interupt if yes (to be honest i have make everywhere a interupt so im surt its not alive anymore) it will interupt my thread and break but when i click an pve another time the same thing goes the last thread is not interupted
 
Piet Souris
Saloon Keeper
Posts: 4505
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, I looked at your code and I do not see why it shouldn't work. Two remarks:

in the actionListener of restart, do you ivoke 'myThread_pve.start()?  And while the scanner is waiting for input, the program will not react on a button_interrupt click.

But I have seen quite some codesnippets now, and I'm losing oversight a bit. Can you show the full code, so that we can run the code (and about the png's: can you specify the dimensions of board and icons?)

And o ja: if round == 1, you should also check that the field is not occupied (like you do when round == 2).
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sure Piet and about the round i never have a bug or other so i didnt care but yeah i will make a proper fix

here my windowSetting class

 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my GUI class


and my memory edit class


and here my png if you want

https://ibb.co/r3jqCnv
https://ibb.co/DbpKP44
https://ibb.co/1J2P5ff
https://ibb.co/StZgcyw
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dont need to specify the png dim if you have it ;)
 
Tim Moores
Saloon Keeper
Posts: 6976
164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's confusing to have so many static methods, because it's not clear if they're meant to be invoked statically, or on an object instance. Nevertheless, this

should just be

because at that point interrupt has already been called; calling it again does nothing.

Also, I would stay away from something like "myThread_pve.interrupted()" - interrupted() is a static method of the Thread class, so calling it like "Thread.interrupted()" makes that clearer. Or you can do it like "myThread_pve.isInterrupted()" to make it clear that you care about that Thread in particular, not just whatever is the current thread.
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tim thank you for your reply yes your are true about all this static method i should need to make a more clear code and i have make a little printf in my if statement before the break and it never spawn so he never go in my in statement so my myThread_pve.interrupt(); in my restart never work
 
Stephan van Hulst
Saloon Keeper
Posts: 12997
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim, personally I think it's good practice to immediately interrupt the current thread again after checking interrupted(). The reason for this is that the calling method could still have a blocking operation. From the controlling thread, you want to call interrupt() (or rather, Future.cancel()) only once and make the entire task unwind without it still blocking somewhere.

An alternative is to not break out of the operation, but to throw an exception:
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even with a throw is dont stop my little printf never show argh this is going to make me crazy
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow wait i think its good now yes its good i can stop my thread now just a little erro from a no closed scanner but hmm i dont really get why its work i have made what Tim asked me a if statement in my method to check my thread (but i have done it previously and dont work) in the end when i send an interupt ordred to my thread ans check in my methode if the thread is interupted it just dont work (and i really dont understand why but ok) so i made a boolean exit(now its a int because a if(bool) dont work either why i dont know) to check it in my methode if exit is 1 then mythread.interupt and just below i have a if thread.interupted() then break and its work now i just need to make a new thread when i click on pve without the scaner error thanks for everyone help your are the best
 
Piet Souris
Saloon Keeper
Posts: 4505
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes!! Got your code running!

That is the good news. The bad news is that the code is buggy, and working with a frame and typing coordinates on the command line is a nightmare!

First of all: what is the difference between this pvp and that pve?

If you click on pvp, then do a restart and then click pvp again, you get an error about an illegal state. The cause is clear, but lets leave that for now.

The pve button works as it should. However, if I enter 0 and then 0, and then 0 and 2, I get the error that I cannot take the position of another player! And in another case, when I entered three coordinate pairs, I was declared winner and this line was printed three times:

GG2 you win you can take a Maximator

and when I press 'restart and then pve, I get the error "No such elementException".

Well, so far my first impressions. I had a math teacher who used to say "a mile on seven" whenever we did someting simple in a complicated way. But for coming up with the idea of creating png's and using Threads AND getting it working, that deserves a cow!

Waiting for your comment and tests!
 
Cyan Rick
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah really buggy and trash the true need to be speak when i will end this i need to remake a full code more safe and good to read and debug

the change in the pvp is juste that the player will play one vs an other user the prog will just ask for X and O to choose the co

i have made my change on pve btn and methode no pvp for now because of the test

yes im fixing this litle bug and im actually stoping using the console i will use the mouseClicked event to get the x and y co and convert them to the co of my game for my method

and haha im not sure if i understand what you want to say with your last sentence but yeah the true is when i was in the early of my code i was thinking if it isnt more simple to make it without png but i really want to do it withe png to know just how it work and if its not better then drawing ^^

i will probably post the full code in one week if all work then mark this thread as resolved at that time if not then you well see me soon x)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic