Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

stop a running thread without exiting the jvm  RSS feed

 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
was wondering whats the best way of stopping a thread without exiting the JVM. I have a java programme which spawns out a few threads in the jvm , each of these threads do different things and i need to stop one of the threads when file name already exists while the other threads continue to run :





thanks in advance.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just return from the run() method.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply. In the distributeTask class, is there a way to detect if the fileCreate thread returned as a result of file already existing ??
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not the way you wrote it, no. Of course a thread is like any other class, it can provide methods to allow other objects to find out about its internal state. In your case you would write a wasFileFound() method which returns a boolean value which you set after looking for the file.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok heres what i got so far :



for some reason i always get a "new file was created" in this, even though i have manually created the file and placed it in the assumed dir :



any suggestions what i am doing wrong ??
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks suspicious:
if(task = 1 ) {
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the task variable is just to read the input since there are multiple tasks which can be done by this class.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh i see what you mean sorry that's a typo, it should be task == 1
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!